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

Stable output: Add some more names to anonymous Method classes #169

Merged
merged 10 commits into from
Jan 19, 2024
7 changes: 6 additions & 1 deletion pkg/codegen/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func (p *Package) Generate(out *Emitter) {
sort.Slice(sorted, func(i, j int) bool {
if a, ok := sorted[i].(Named); ok {
if b, ok := sorted[j].(Named); ok {
return a.GetName() < b.GetName()
return schemas.CleanNameForSorting(a.GetName()) < schemas.CleanNameForSorting(b.GetName())
}
}

Expand Down Expand Up @@ -168,6 +168,11 @@ func (f Fragment) Generate(out *Emitter) {
// Method defines a method and how to generate it.
type Method struct {
Impl func(*Emitter)
Name string
}

func (m *Method) GetName() string {
return m.Name
}

func (m *Method) Generate(out *Emitter) {
Expand Down
5 changes: 4 additions & 1 deletion pkg/generator/schema_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ func (g *schemaGenerator) generateDeclaredType(

g.output.file.Package.AddDecl(&codegen.Method{
Impl: formatter.generate(decl, validators),
Name: decl.GetName() + "_validator",
})
}
}
Expand Down Expand Up @@ -742,7 +743,7 @@ func (g *schemaGenerator) generateEnumType(

if !g.config.OnlyModels {
valueConstant := &codegen.Var{
Name: "enumValues_" + enumDecl.Name,
Name: schemas.PrefixEnumValue + enumDecl.Name,
Value: t.Enum,
}
g.output.file.Package.AddDecl(valueConstant)
Expand All @@ -758,11 +759,13 @@ func (g *schemaGenerator) generateEnumType(
if wrapInStruct {
g.output.file.Package.AddDecl(&codegen.Method{
Impl: formatter.enumMarshal(enumDecl),
Name: enumDecl.GetName() + "_enum",
})
}

g.output.file.Package.AddDecl(&codegen.Method{
Impl: formatter.enumUnmarshal(enumDecl, enumType, valueConstant, wrapInStruct),
Name: enumDecl.GetName() + "_enum_unmarshal",
})
}
}
Expand Down
11 changes: 11 additions & 0 deletions pkg/schemas/types.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package schemas

import "strings"

const (
TypeNameString = "string"
TypeNameArray = "array"
Expand All @@ -8,6 +10,7 @@ const (
TypeNameObject = "object"
TypeNameBoolean = "boolean"
TypeNameNull = "null"
PrefixEnumValue = "enumValues_"
)

func IsPrimitiveType(t string) bool {
Expand All @@ -19,3 +22,11 @@ func IsPrimitiveType(t string) bool {
return false
}
}

func CleanNameForSorting(name string) string {
if strings.HasPrefix(name, PrefixEnumValue) {
return strings.TrimPrefix(name, PrefixEnumValue) + "_enumValues" // Append a string for sorting properly.
}

return name
}
10 changes: 5 additions & 5 deletions tests/data/core/date/date.go

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

10 changes: 5 additions & 5 deletions tests/data/core/dateTime/dateTime.go

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

10 changes: 5 additions & 5 deletions tests/data/core/ip/ip.go

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

10 changes: 5 additions & 5 deletions tests/data/core/object/object.go

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

16 changes: 8 additions & 8 deletions tests/data/core/refToEnum/refToEnum.go

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

10 changes: 5 additions & 5 deletions tests/data/core/time/time.go

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

46 changes: 23 additions & 23 deletions tests/data/extraImports/gopkgYAMLv3/gopkgYAMLv3.go

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

Loading