mholt / json-to-go

Translates JSON into a Go type in your browser instantly (original)

Home Page:https://mholt.github.io/json-to-go/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Handle cases where json.RawMessage should be used instead.

zet4 opened this issue · comments

So in my code I have a config file in the form of

{
    "destinations": [
        {
            "username": "Catgirls are sexy",
            "avatar": "https://b.catgirlsare.sexy/xik9.png",
            "webhook": "your discord webhook url goes here",
            "cron": "0/5 * * * * *",
            "sources": [
                {
                    "service": "randomcat",
                    "chance": 1,
                    "arguments": {"something": "different"}
                },
                {
                    "service": "ibsearch",
                    "chance": 2,
                    "arguments": {"query": "nekomimi rating:safe random: size:<2m>1m", "key": "ibsearch key goes here"}
                },
                {
                    "service": "ibsearch",
                    "chance": 2,
                    "arguments": {"query": "fox_ears rating:safe random: size:<2m>1m", "key": "ibsearch key goes here"}
                }
            ] 
        }
    ]
}

The point here is, each source is handled by it's service, those in turn may opt in to read optional arguments from it's definition. Which you can see for ibsearch I have defined.
What I get when I run this through your tool is I get

type AutoGenerated struct {
	Destinations []struct {
		Username string `json:"username"`
		Avatar string `json:"avatar"`
		Webhook string `json:"webhook"`
		Cron string `json:"cron"`
		Sources []struct {
			Service string `json:"service"`
			Chance int `json:"chance"`
			Arguments struct {
				Something string `json:"something"`
			} `json:"arguments"`
		} `json:"sources"`
	} `json:"destinations"`
}

What I ideally would prefer it would generate is:

type AutoGenerated struct {
	Destinations []struct {
		Username string `json:"username"`
		Avatar string `json:"avatar"`
		Webhook string `json:"webhook"`
		Cron string `json:"cron"`
		Sources []struct {
			Service string `json:"service"`
			Chance int `json:"chance"`
			Arguments json.RawMessage `json:"arguments"`
		} `json:"sources"`
	} `json:"destinations"`
}

Because it would see that same structure nodes contain differing data and a uniform structure can't be constructed hence it should be handled downstream (via json.RawMessage).

Is this no longer wanted?

Just cleaning up my issue backlog, I personally don't have a need for it anymore and nobody else touched this in the last 2 years.