Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge duplicate struct definitions #65

Open
DCRichards opened this issue May 10, 2019 · 6 comments
Open

Merge duplicate struct definitions #65

DCRichards opened this issue May 10, 2019 · 6 comments

Comments

@DCRichards
Copy link

DCRichards commented May 10, 2019

Where there are multiple properties which reuse the same nested properties, the corresponding struct definition should be flattened. Take the following JSON:

{
   "filter":{
      "sitereference":[
         {
            "value":"shshs"
         }
      ],
      "transactionreference":[
         {
            "value":"3-64-25205"
         }
      ]
   }
}

Expected output:

type AutoGenerated struct {
	Filter Filter `json:"filter"`
}

type FilterAutoGenerated struct {
	Value string `json:"value"`
}

type Filter struct {
	Sitereference        []FilterAutoGenerated `json:"sitereference"`
	Transactionreference []FilterAutoGenerated `json:"transactionreference"`
}

Actual output:

type AutoGenerated struct {
	Filter Filter `json:"filter"`
}

type Sitereference struct {
	Value string `json:"value"`
}

type Transactionreference struct {
	Value string `json:"value"`
}

type Filter struct {
	Sitereference        []Sitereference        `json:"sitereference"`
	Transactionreference []Transactionreference `json:"transactionreference"`
}
@michaelsanford
Copy link

michaelsanford commented May 26, 2019

I just encountered this and came to file this exact issue.

I'd be willing to take a look (given the tag).

Incidentally, this tool is a boon to anyone using a public API that produces extremely verbose structures. Thank you! ❤️ 🙏

@mholt
Copy link
Owner

mholt commented May 26, 2019

@michaelsanford Yes, would be happy to have your contribution! Thank you!

@Capable-Disk4147
Copy link

@michaelsanford Are you still working on this?

@michaelsanford
Copy link

@Capable-Disk4147 My gosh, this one slipped under my radar. Many apologies: I am not.

@DCRichards
Copy link
Author

I should be able to take a look, but I'm AFK a bit ATM so if someone else is keen then feel free

@emicklei
Copy link

I encountered the same problem, here is my simplified example for inspiration.

{
    "dimensionCounts": [
        {
            "label": {
                "de_de": "Mitarbeiter",
                "ja_jp": "\u5F93\u696D\u54E1"
            }
        },
        {
            "label": {
                "fr_fr": "Qualifications",
                "de_de": "Qualifikationen"
            }
        }
    ]
}
DimensionCounts []struct {
	Label struct {
		DeDe string `json:"de_de"`
		JaJp string `json:"ja_jp"`
		FrFr string `json:"fr_fr"`
	} `json:"label,omitempty"`
} `json:"dimensionCounts"`

I will have a look at the code later to see if I can help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants