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.48k stars 473 forks source link

fix duplicate top-level structs #131

Closed grische closed 1 month ago

grische commented 1 month ago

Before, the attached example was converted to:

type Identifier struct {
        Type string `json:"type"`
        ID int `json:"id"`
}
type Identifier struct {
        Type string `json:"type"`
        Name string `json:"name"`
}
type Identifier struct {
        Postal Postal `json:"postal"`
        Road Road `json:"road"`
}

This PR prefixed known structs with the parent and if that's not enough, appends an integer:

type Identifier struct {
    Type string `json:"type"`
    ID int `json:"id"`
}
type MunicipalityIdentifier struct {
    Type string `json:"type"`
    Name string `json:"name"`
}
type BuildingIdentifier struct {
    Postal Postal `json:"postal"`
    Road Road `json:"road"`
}

The second commit improves (and fixes) the naming of deeper nested duplicate objects. I think it's best to keep the commits separate and not squash them on merge.

The tests also depend on https://github.com/mholt/json-to-go/pull/128

Fixes #78

mholt commented 1 month ago

Thank you! After merge conflicts are resolved I can merge (not squash).

grische commented 1 month ago

@mholt thanks for the quick response. I rebased the commit and fixed the merge conflicts.