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

Output squashed structs #280

Draft
wants to merge 1 commit into
base: feature/implement-subschemas
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions go.work.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
github.com/atombender/go-jsonschema/tests/data v0.0.0-20231003003002-2b73c089a581/go.mod h1:kLoRQLRVy+GT9/PG2e3u31DPvDmtFEn7pX6FItvbqlA=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.10.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM=
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
Expand All @@ -11,7 +12,6 @@ github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5t
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
github.com/atombender/go-jsonschema/tests/data v0.0.0-20231003003002-2b73c089a581/go.mod h1:kLoRQLRVy+GT9/PG2e3u31DPvDmtFEn7pX6FItvbqlA=
golang.org/x/mod v0.13.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/mod v0.14.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
Expand All @@ -24,14 +24,15 @@ golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o=
golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58=
golang.org/x/tools v0.14.0/go.mod h1:uYBEerGOWcJyEORxN+Ek8+TT266gXkNlHdJBwexUsBg=
golang.org/x/tools v0.17.0/go.mod h1:xsh6VxdV005rRVaS6SSAf9oiAqljS7UZUacMZ8Bnsps=
golang.org/x/tools v0.20.0/go.mod h1:WvitBU7JJf6A4jOdg4S1tviW9bhUxkgeCui/0JHctQg=
golang.org/x/tools v0.21.0/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
68 changes: 68 additions & 0 deletions pkg/generator/schema_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,74 @@ func (g *schemaGenerator) generateStructType(t *schemas.Type, scope nameScope) (
structType.AddField(structField)
}

for _, s := range t.AllOf {
structFieldType, err := g.generateTypeInline(s, scope)
if err != nil {
return nil, fmt.Errorf("could not generate type for field: %w", err)
}

//TODO: Support is required?
// isRequired := requiredNames[name]

//TODO: Pick a name for the field
// fieldName := g.caser.Identifierize("Test")

// if ext := prop.GoJSONSchemaExtension; ext != nil {
// for _, pkg := range ext.Imports {
// g.output.file.Package.AddImport(pkg, "")
// }

// if ext.Identifier != nil {
// fieldName = *ext.Identifier
// }
// }

// if count, ok := uniqueNames[fieldName]; ok {
// uniqueNames[fieldName] = count + 1
// fieldName = fmt.Sprintf("%s_%d", fieldName, count+1)
// g.warner(fmt.Sprintf("Field %q maps to a field by the same name declared "+
// "in the same struct; it will be declared as %s", name, fieldName))
// } else {
// uniqueNames[fieldName] = 1
// }

structField := codegen.StructField{
Name: "",
// Comment: prop.Description,
JSONName: "",
// SchemaType: prop,
Type: structFieldType,
}

tags := ""

for _, tag := range g.config.Tags {
//TODO: Make this work for yaml too. It uses "inline" instead of "squash".
tags += fmt.Sprintf(`%s:",squash" `, tag)
}

structField.Tags = strings.TrimSpace(tags)

// if structField.Comment == "" {
// structField.Comment = fmt.Sprintf("%s corresponds to the JSON schema field %q.",
// structField.Name, name)
// }

// switch {
// case prop.Default != nil:
// structField.DefaultValue = g.defaultPropertyValue(prop)

// default:
// if isRequired {
// structType.RequiredJSONFields = append(structType.RequiredJSONFields, structField.JSONName)
// } else if !structField.Type.IsNillable() {
// structField.Type = codegen.WrapTypeInPointer(structField.Type)
// }
// }

structType.AddField(structField)
}

// Checking .Not here because `false` is unmarshalled to .Not = Type{}.
if t.AdditionalProperties != nil && t.AdditionalProperties.Not == nil {
if len(t.AdditionalProperties.Type) > 1 {
Expand Down
8 changes: 8 additions & 0 deletions tests/data/squashedStructs/imported.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions tests/data/squashedStructs/imported.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"id": "https://example.com/imported",
"type": "object",
"properties": {
"importedString": {
"type": "string"
}
}
}
15 changes: 15 additions & 0 deletions tests/data/squashedStructs/squashed.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions tests/data/squashedStructs/squashed.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"id": "https://example.com/objectAllOf",
"type": "object",
"allOf": [
{
"$ref": "imported.json"
}
],
"properties": {
"str": {
"type": "string"
}
}
}
Loading