-
-
Save daenney/07e546e0c9d26e5ba183feadd3ba00c4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/xml" | |
"fmt" | |
"time" | |
) | |
var dataSingle = ` | |
<response> | |
<info> | |
<lastmodified>2024-01-14T10:53:21Z</lastmodified> | |
</info> | |
<a><id>1</id></a> | |
</response> | |
` | |
var dataMultiple = ` | |
<response> | |
<info> | |
<lastmodified>2024-01-14T10:53:21Z</lastmodified> | |
</info> | |
<a><id>1</id></a> | |
<a><id>2</id></a> | |
</response> | |
` | |
type A1 struct { | |
ID string `xml:"id"` | |
} | |
type Response1 struct { | |
XMLName xml.Name `xml:"response"` | |
A []A1 `xml:"a"` | |
LastModified *time.Time `xml:"info>lastmodified"` | |
} | |
type Response2 struct { | |
XMLName xml.Name `xml:"response"` | |
Data []A1 `xml:"a"` | |
LastModified *time.Time `xml:"info>lastmodified"` | |
} | |
type A3 struct { | |
XMLName xml.Name `xml:"a"` | |
ID string `xml:"id"` | |
} | |
type Response3 struct { | |
XMLName xml.Name `xml:"response"` | |
Data []A3 | |
LastModified *time.Time `xml:"info>lastmodified"` | |
} | |
type Response4[T Responses4] struct { | |
XMLName xml.Name `xml:"response"` | |
Data []T | |
LastModified *time.Time `xml:"info>lastmodified"` | |
} | |
type Responses4 interface { | |
A4 | RespB | |
} | |
type ( | |
A4 struct { | |
XMLName xml.Name `xml:"a"` | |
ID string `xml:"id"` | |
} | |
RespB struct{} | |
) | |
type Response5[T Responses4] struct { | |
XMLName xml.Name `xml:"response"` | |
Data []T `xml:",any"` | |
LastModified *time.Time `xml:"info>lastmodified"` | |
} | |
func main() { | |
fmt.Println("example 1") | |
decodeAndPrint(dataSingle, &Response1{}) | |
decodeAndPrint(dataMultiple, &Response1{}) | |
fmt.Println("") | |
fmt.Println("example 2") | |
decodeAndPrint(dataSingle, &Response2{}) | |
decodeAndPrint(dataMultiple, &Response2{}) | |
fmt.Println("") | |
fmt.Println("example 3") | |
decodeAndPrint(dataSingle, &Response3{}) | |
decodeAndPrint(dataMultiple, &Response3{}) | |
fmt.Println("") | |
fmt.Println("example with generics") | |
decodeAndPrint(dataSingle, &Response4[A4]{}) | |
decodeAndPrint(dataMultiple, &Response4[A4]{}) | |
fmt.Println("") | |
fmt.Println("example with generics and xml any tag") | |
decodeAndPrint(dataSingle, &Response5[A4]{}) | |
decodeAndPrint(dataMultiple, &Response5[A4]{}) | |
} | |
func decodeAndPrint(data string, res any) { | |
if err := xml.Unmarshal([]byte(data), res); err != nil { | |
fmt.Println(err) | |
return | |
} | |
fmt.Printf("%+v\n", res) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
example 1 | |
&{XMLName:{Space: Local:response} A:[{ID:1}] LastModified:2024-01-14 10:53:21 +0000 UTC} | |
&{XMLName:{Space: Local:response} A:[{ID:1} {ID:2}] LastModified:2024-01-14 10:53:21 +0000 UTC} | |
example 2 | |
&{XMLName:{Space: Local:response} Data:[{ID:1}] LastModified:2024-01-14 10:53:21 +0000 UTC} | |
&{XMLName:{Space: Local:response} Data:[{ID:1} {ID:2}] LastModified:2024-01-14 10:53:21 +0000 UTC} | |
example 3 | |
&{XMLName:{Space: Local:response} Data:[] LastModified:2024-01-14 10:53:21 +0000 UTC} | |
&{XMLName:{Space: Local:response} Data:[] LastModified:2024-01-14 10:53:21 +0000 UTC} | |
example with generics | |
&{XMLName:{Space: Local:response} Data:[] LastModified:2024-01-14 10:53:21 +0000 UTC} | |
&{XMLName:{Space: Local:response} Data:[] LastModified:2024-01-14 10:53:21 +0000 UTC} | |
example with generics and xml any tag | |
&{XMLName:{Space: Local:response} Data:[{XMLName:{Space: Local:a} ID:1}] LastModified:2024-01-14 10:53:21 +0000 UTC} | |
&{XMLName:{Space: Local:response} Data:[{XMLName:{Space: Local:a} ID:1} {XMLName:{Space: Local:a} ID:2}] LastModified:2024-01-14 10:53:21 +0000 UTC} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment