The package for creating and modifying a structure in runtime
This package allows:
- to change tags to fields of external structure.
- to build structure.
- to merge two structure.
- to fill structure from another structure or map
- to fill another structure or map from structure
structure "github.com/irbgeo/go-structure"- Create structure interface from external structure
b, _ := structure.New(new(pkg.Structure))- Change tags in structure
b.ChangeTags(getNewTag)
func getNewTag(fieldName, fieldTag, fieldType string) string {
return strings.ToLower(fieldName)
}- Fill structure from yaml
err = yaml.Unmarshal([]byte("content for pkg.Structure"), b.Struct())- Fill structure from another struct
var dst pkg.Structure
err = b.AssignFrom(&dst)- Save structure data into external structure
var dst pkg.Structure
err = b.SaveInto(&dst)- Create structure builder
builder := structure.NewBuilder()- Add fields to structure
builder.AddField("Field1", "example-string", `yaml:"field1"`)
builder.AddField("Field2", 1, `yaml:"field2"`)
builder.AddField("Field3", false, `yaml:"field3"`)- Build structure interface
st := builder.Build()Merge two different structs.
src := testSrcStructure{
Field1: "src-field1",
Field2: 1,
Field3: 1,
Field5: "src-field5",
}
dst := testDstStructure{
Field1: "dst-field1",
Field2: "dst-field2",
Field3: 2,
Field4: "dst-field4",
}
err := structure.Merge(&dst, &src)
// result dst
// testDstStructure{
// Field1: "src-field1",
// Field2: "dst-field2",
// Field3: 1,
// Field4: "dst-field4",
// } src := testStructureWithoutTag{
Field1: "test-value",
Field2: 3,
Field3: true,
}
dst := make(map[string]any)
err := structure.SaveStructToMap(dst, &src)
// result dst
// map[string]any{
// "Field1": "test-value",
// "Field2": 3,
// "Field3": true,
// } src := map[string]any{
"Field1": "test-value",
"Field2": 3,
"Field3": true,
}
var dst testStructureWithoutTag
err := structure.AssignStructFromMap(&dst, src)
// result dst
// testStructureWithoutTag{
// Field1: "test-value",
// Field2: 3,
// Field3: true,
// }- Benchmarks
- to add tag to substructs