mholt/json-to-go

Merge duplicate struct definitions

Open

#65 opened on May 10, 2019

 (6 comments) (2 reactions) (0 assignees)JavaScript (478 forks)batch import
help wanted

Repository metrics

Stars
 (4,625 stars)
PR merge metrics
 (No merged PRs in 30d)

Description

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"`
}

Contributor guide