Code Monkey home page Code Monkey logo

go-jsonstruct's Issues

Add support for `,string` annotation

As identified by @breml at the Bärner Go Meetup:

encoding/json supports a ,string struct tag annotation that allows strings to be interpreted as ints, float64s, bools, and strings. go-jsonstruct should support this annotation.

Extract nested structs

Currently all nested structs are generated as literal structs within the parent struct identical to the data being parsed. Is it possible to make go-jsonstruct extract such nested struct definitions to named structs which type is then used as a field instead of the nesting of the struct literals? This would make it easier in some cases to create instances for posting to REST-api's, as otherwise I have to repeat the nested type definition when specifying the value.

E.g based on input

{
  "items": [
    {
      "datapoints": [
        {
          "timestamp": 1687907013216,
          "value": 42.54
        },
        {
          "timestamp": -149967180845,
          "value": 1337.43
        }
      ],
      "id": 6361985487601820
    }
  ]
}

currently this is generated

type InsertDataPointsRequest struct {
        Items []struct {
                Datapoints []struct {
                        Timestamp int64   `json:"timestamp"`
                        Value     float64 `json:"value"`
                } `json:"datapoints"`
                ID int64 `json:"id"`
        } `json:"items"`
}

But by exploding the nested structs it could look like this

type Datapoint struct {
        Timestamp int64   `json:"timestamp"`
        Value     float64 `json:"value"`
}

type Item struct {
        Datapoints []Datapoint `json:"datapoints"`
        ID int64 `json:"id"`
}

type InsertDataPointsRequest struct {
        Items []item `json:"items"`
}

Which would make the use of the code go from this

	v := InsertDataPointsRequest{
		Items: []struct {
			Datapoints []struct {
				Timestamp int64   `json:"timestamp"`
				Value     float64 `json:"value"`
			} `json:"datapoints"`
			ID int64 `json:"id"`
		}{{
			Datapoints: []struct {
				Timestamp int64   `json:"timestamp"`
				Value     float64 `json:"value"`
			}{
				{
					Timestamp: 12,
					Value:     53,
				},
			},
			ID: 42,
		}},
	}
    
}

to this

	v := InsertDataPointsRequest{
		Items: []Item{{
			Datapoints: []Datapoint{
				{
					Timestamp: 12,
					Value:     53,
				},
			},
			ID: 42,
		},
		},
	}

Add an option to read json from file

rationale: I would like to use go-jsonstruct in a //go:generate-directive. However the documentation says:

The arguments to the directive are space-separated tokens or double-quoted strings passed to the generator as individual arguments when it is run.

So constructs like gojsonstruct < file.json or cat file.json | gojsonstruct will not work. Instead I have to use

//go:generate sh -c "gojsonstruct -packagename $GOPACKAGE < ./file.json"

Which is not portable. If gojsonstruct would be able to read from file the above generator statement could be made environment neutral.

Add AbsentOrNullValue as a type

Currently, go-jsonstruct is unable to distinguish between the following cases:

{"foo":"bar"} // present string value
{"foo":""}    // present but empty string value
{"foo":null}  // present null value
{}            // absent value

go-jsonstruct should be able to handle these sorts of values, maybe using a technique similar to database/sql.NullString.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.