Skip to content

Go struct utilities with reflection for JSON data decoding, map-liked data accessing, dynamic struct building and more

License

Notifications You must be signed in to change notification settings

schieberegister/structil

 
 

Repository files navigation

structil PkgGoDev

Workflow Status Go Report Card Codecov MIT License

struct + util = structil, for runtime and dynamic environment in Go.

Why?

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.

Simple Usage

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 nest is true, nested object attributes will be also decoded to struct recursively
  • If useTag is true, JSON Struct tags are defined

And see example code.

More Examples

DynamicStruct

We can create the dynamic and runtime struct.

See example code

JSON unmershal with DynamicStruct

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

Getter

We can access a struct using field name string, like (typed) map.

See example code

MapGet method

MapGet method provides the Map collection function for slice of struct

See example code

Finder

We can access usefully nested struct fields using field name string.

See example code

With config file? use FinderKeys

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.

Benchmark

See this file

It's the latest benchmark result that is executed on GitHub Actions runner instance.

About

Go struct utilities with reflection for JSON data decoding, map-liked data accessing, dynamic struct building and more

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Go 96.5%
  • Makefile 3.0%
  • Other 0.5%