struct + util = structil, for runtime and dynamic environment in Go.
I'd like to ...
- conveniently handle and decode the known or unknown formatted JSON/YAML
- conveniently dive into the specific field in nested struct
- simply verify if a field with the specified name and type exists in object
- etc
with Go reflection package experimentally.
*** JSON and YAML format is known or unknown ***
JSON →→→→→→→→→→→→→→→→↓ →→ (known format) struct →→→→→→→→→→→↓→→→ (use struct directly)
↓ ↑ ↓
↓→→ map →→→ (unknown format) "DynamicStruct" →→→→→→ "Getter", "Finder"
↑
YAML →→→→→→→→→→→→→→→→↑
↑
(and other formats) →↑
Please see my medium post as well.
Try printing the struct definition from the unknown formatted JSON decoding.
package main
import (
"fmt"
"github.com/goldeneggg/structil/dynamicstruct/decoder"
)
func main() {
unknownJSON := []byte(`
{
"string_field":"かきくけこ",
"int_field":45678,
"bool_field":false,
"object_field":{
"id":12,
"name":"the name",
"nested_object_field": {
"address": "Tokyo",
"is_manager": true
}
},
"array_string_field":[
"array_str_1",
"array_str_2"
],
"array_struct_field":[
{
"kkk":"kkk1",
"vvvv":"vvv1"
},
{
"kkk":"kkk2",
"vvvv":"vvv2"
}
],
"null_field":null
}
`)
jsonDec, err := decoder.NewJSON(unknownJSON)
if err != nil {
panic(err)
}
nest := true
useTag := true
ds, err := jsonDec.DynamicStruct(nest, useTag)
if err != nil {
panic(err)
}
// Print struct definition from DynamicStruct
fmt.Println(ds.Definition())
}This program will print a Go struct definition string as follows.
type DynamicStruct struct {
ArrayStringField []string `json:"array_string_field"`
ArrayStructField []struct {
Kkk string `json:"kkk"`
Vvvv string `json:"vvvv"`
} `json:"array_struct_field"`
BoolField bool `json:"bool_field"`
IntField float64 `json:"int_field"`
NullField interface {} `json:"null_field"`
ObjectField struct {
Id float64 `json:"id"`
Name string `json:"name"`
NestedObjectField struct {
Address string `json:"address"`
IsManager bool `json:"is_manager"`
} `json:"nested_object_field"`
} `json:"object_field"`
StringField string `json:"string_field"`
}
- Type name is "DynamicStruct"
- Field names are automatically camelized from input json attribute names
- Fields are ordered by field name
- If
nestis true, nested object attributes will be also decoded to struct recursively - If
useTagis true, JSON Struct tags are defined
And see example code.
We can create the dynamic and runtime struct.
See example code
A decoding example from JSON to DynamicStruct with StructTag using json.Unmarshal([]byte) as follows.
This example works correctly not only JSON but also YAML, TOML and more.
See example code
We can access a struct using field name string, like (typed) map.
See example code
MapGet method provides the Map collection function for slice of struct
See example code
We can access usefully nested struct fields using field name string.
See example code
We can create a Finder from the configuration file that have some finding target keys. We support some file formats of configuration file such as yaml, json, toml and more.
See example code
Thanks for the awesome configuration management library spf13/viper.
See this file
It's the latest benchmark result that is executed on GitHub Actions runner instance.