mholt / json-to-go

Translates JSON into a Go type in your browser instantly (original)
https://mholt.github.io/json-to-go/
MIT License
4.51k stars 475 forks source link

struct body parse miss #55

Closed godcong closed 5 years ago

godcong commented 5 years ago

for example: { "mounts": [ { "child": { "path": "blocks", "shardFunc": "/repo/flatfs/shard/v1/next-to-last/2", "sync": true, "type": "flatfs" }, "mountpoint": "/blocks", "prefix": "flatfs.datastore", "type": "measure" }, { "child": { "compression": "none", "path": "datastore", "type": "levelds" }, "mountpoint": "/", "prefix": "leveldb.datastore", "type": "measure" } ], "type": "mount" }

parsed struct: type AutoGenerated struct { Mounts []struct { Child struct { Path json:"path" ShardFunc json:"shardFunc" Sync json:"sync" Type json:"type" } json:"child" Mountpoint json:"mountpoint" Prefix json:"prefix" Type json:"type" } json:"mounts" Type string json:"type" }

the second child's element compression was missed

mike-hosseini commented 5 years ago

I looked into this, the issue is not caused by any of the recent changes.

As a workaround, I renamed elements named child, to child1 and child2 and this is the output I got:

type AutoGenerated struct {
        Mounts []struct {
                Child1 struct {
                        Path string `json:"path"`
                        ShardFunc string `json:"shardFunc"`
                        Sync bool `json:"sync"`
                        Type string `json:"type"`
                } `json:"child1,omitempty"`
                Mountpoint string `json:"mountpoint"`
                Prefix string `json:"prefix"`
                Type string `json:"type"`
                Child2 struct {
                        Compression string `json:"compression"`
                        Path string `json:"path"`
                        Type string `json:"type"`
                } `json:"child2,omitempty"`
        } `json:"mounts"`
        Type string `json:"type"`
}
mholt commented 5 years ago

@mahdi-hosseini I'm going to send you an invite to be a collaborator on this project, that way you can have rights to merge PRs into master and even update gh-pages without having to wait for me. :+1:

mike-hosseini commented 5 years ago

@mholt Awesome, thanks! The goal is to segue to maintaining caddy 😛

mike-hosseini commented 5 years ago

This is resolved. Make sure to hard refresh or use your browser's private/incognito mode to see the changes.

godcong commented 5 years ago

yes,i tried it.

the result has two child struct:

type AutoGenerated struct {
    Mounts []struct {
        Child struct {
            Path      string `json:"path"`
            ShardFunc string `json:"shardFunc"`
            Sync      bool   `json:"sync"`
            Type      string `json:"type"`
        } `json:"child,omitempty"`
        Mountpoint string `json:"mountpoint"`
        Prefix     string `json:"prefix"`
        Type       string `json:"type"`
        Child      struct {
            Compression string `json:"compression"`
            Path        string `json:"path"`
            Type        string `json:"type"`
        } `json:"child,omitempty"`
    } `json:"mounts"`
    Type string `json:"type"`
}

i think the correct is like this:

type AutoGenerated struct {
    Mounts []struct {
        Child struct {
            Path      string `json:"path,omitempty"`
            ShardFunc string `json:"shardFunc,omitempty"`
            Sync      bool   `json:"sync,omitempty"`
            Type      string `json:"type,omitempty"`
                        Compression string `json:"compression,omitempty"`
        } `json:"child"`
        Mountpoint string `json:"mountpoint"`
        Prefix     string `json:"prefix"`
        Type       string `json:"type"`
    } `json:"mounts"`
    Type string `json:"type"`
}
mike-hosseini commented 5 years ago

@godcong That's by design. I wanted to leave the decision of combining the two structs to the Go developer. In general JSONs can have arbitrary type of data in them. A generalization like that comes with assumptions and trade-offs.

mike-hosseini commented 5 years ago

@mholt Please let me know if you have a different take on my response.