Created
February 16, 2023 21:09
-
-
Save jantytgat/6867b3bcbb8bf7660f55d33729427ec3 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 ( | |
"bytes" | |
"encoding/json" | |
"fmt" | |
"reflect" | |
"strings" | |
) | |
type EmbeddedData struct { | |
Name string `json:"name"` | |
Id int `json:"id"` | |
Content string `json:"content"` | |
} | |
type RootData struct { | |
Name string `json:"name"` | |
Data EmbeddedData `json:"data"` | |
} | |
func main() { | |
// Part 1: Simple struct and serialization | |
fmt.Println("PART1") | |
d1 := EmbeddedData{ | |
Name: "data1", | |
Id: 1, | |
Content: "embeddedData1", | |
} | |
fmt.Printf("Struct: %v\n", d1) | |
w1 := bytes.Buffer{} | |
_ = json.NewEncoder(&w1).Encode(d1) | |
fmt.Printf("JSON: %s\n", w1.String()) | |
// Part 2: Adding embedded data to root data | |
fmt.Println("PART2") | |
d2 := RootData{ | |
Name: "rootdata1", | |
Data: d1, | |
} | |
fmt.Printf("Struct: %v\n", d2) | |
w2 := bytes.Buffer{} | |
_ = json.NewEncoder(&w2).Encode(d2) | |
fmt.Printf("JSON: %s\n", w2.String()) | |
// Part 3: Creating a simple struct using reflection | |
fmt.Println("PART3") | |
t3 := reflect.StructOf([]reflect.StructField{ | |
{ | |
Name: "Name", | |
Type: reflect.TypeOf(""), | |
}, | |
{ | |
Name: "Data", | |
Type: reflect.TypeOf(d1), | |
}, | |
}) | |
d3 := reflect.New(t3).Elem() | |
d3.Field(0).SetString("data3") | |
d3.Field(1).Set(reflect.ValueOf(d1)) | |
v3 := d3.Addr().Interface() | |
fmt.Printf("Struct: %v\n", d3) | |
w3 := bytes.Buffer{} | |
_ = json.NewEncoder(&w3).Encode(v3) | |
fmt.Printf("JSON: %s\n", w3.String()) | |
// Part 4: Creating a simple struct through a function using reflection | |
fmt.Println("PART4") | |
d4 := GetData4("Data4", d1) | |
fmt.Printf("Struct: %v\n", d4) | |
w4 := bytes.Buffer{} | |
_ = json.NewEncoder(&w4).Encode(d4) | |
fmt.Printf("JSON: %s\n", w4.String()) | |
// Part 5: Creating a simple struct through a function using reflection | |
fmt.Println("PART5") | |
data5 := []interface{}{ | |
d1, | |
d1, | |
d1, | |
} | |
d5 := GetData5("Data5", data5) | |
fmt.Printf("Struct: %v\n", d5) | |
w5 := bytes.Buffer{} | |
_ = json.NewEncoder(&w5).Encode(d5) | |
fmt.Printf("JSON: %s\n", w5.String()) | |
} | |
func GetData4(name string, data interface{}) interface{} { | |
tag := strings.ToLower(`json:"` + name + `"`) | |
t := reflect.StructOf([]reflect.StructField{ | |
{ | |
Name: "Name", | |
Type: reflect.TypeOf(data), | |
Tag: reflect.StructTag(tag), | |
}, | |
}) | |
d := reflect.New(t).Elem() | |
d.Field(0).Set(reflect.ValueOf(data)) | |
return d.Addr().Interface() | |
} | |
func GetData5(name string, data []interface{}) interface{} { | |
tag := strings.ToLower(`json:"` + name + `"`) | |
t := reflect.StructOf([]reflect.StructField{ | |
{ | |
Name: "Name", | |
Type: reflect.TypeOf(data), | |
Tag: reflect.StructTag(tag), | |
}, | |
}) | |
d := reflect.New(t).Elem() | |
d.Field(0).Set(reflect.ValueOf(data)) | |
return d.Addr().Interface() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output: