-
Notifications
You must be signed in to change notification settings - Fork 34
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
Extract nested structs #17
Comments
I would like to implement this. go-xmlstruct already has it (the
Consider the following: {
"bounds": {
"topLeft": {
"x": 1,
"y": 2
},
"bottomRight": {
"x": 3,
"y": 4,
"z": 5
}
} The desired Go output would be something like: package main
type Point struct {
X int `json:"x"`
Y int `json:"y"`
Z int `json:"z,omitempty"`
}
type Bounds struct {
BottomRight Point `json:"bottomRight"`
TopLeft Point `json:"topLeft"`
}
type T struct {
Bounds Bounds `json:"bounds"`
} but where would the names Consider also: {
"id": 1,
"type": "person",
"name": "Bob",
"favoriteColor": "red"
}
{
"id": 2,
"type": "car",
"make": "Volkswagen",
"model": "Rabbit"
} should these be a single type with Fundamentally, I suspect that it's not possible to reliably extract subtypes automatically from JSON. So, when I've encountered cases like this I've manually edited the output of |
That for sure is an issue. Maybe it could implemented by using a sort of manually specified name map? where the user provides some sort of mapping between objects and typenames for the types to extract and name? Although it comes with a host of potential complexity. |
By the time you're writing a manually-specified name map you're starting to define a schema. You can define the schema by writing the Go structs yourself, or editing gojsonstruct's output manually, or using JSON Schema. gojsonstruct is useful when you don't have a schema. |
Just to be clear, I think it would be fantastic if gojsonstruct could implement what you're requesting. I'm just not sure how to do it. |
Another example, given: {
"name": "Alice",
"friends": {
"cheshireCat": {
"magical": true
}
}
} You'd probably want something like: type Friend struct {
Magical bool `json:"magical"`
}
type T struct {
Name string `json:"name"`
Friends map[string]Friend `json:"friends"`
} But how do you communicate that Overall, I think what is being requested here is impossible to do automatically. Closing. |
For info, if you're using CUE then https://github.com/sivukhin/cuebootstrap has some neat features for controlling the generated schema. |
Currently all nested structs are generated as literal structs within the parent struct identical to the data being parsed. Is it possible to make go-jsonstruct extract such nested struct definitions to named structs which type is then used as a field instead of the nesting of the struct literals? This would make it easier in some cases to create instances for posting to REST-api's, as otherwise I have to repeat the nested type definition when specifying the value.
E.g based on input
currently this is generated
But by exploding the nested structs it could look like this
Which would make the use of the code go from this
to this
The text was updated successfully, but these errors were encountered: