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

improvement: Enum values expose New() to ease creating new container types #542

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-542.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: improvement
improvement:
description: Enum values expose New() to ease creating new container types
links:
- https://github.com/palantir/conjure-go/pull/542
80 changes: 48 additions & 32 deletions conjure-api/conjure/spec/enums.conjure.go

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

22 changes: 15 additions & 7 deletions conjure-go-verifier/conjure/verification/types/enums.conjure.go

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

14 changes: 13 additions & 1 deletion conjure/enumwriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const (
func writeEnumType(file *jen.Group, enumDef *types.EnumType) {
file.Add(enumDef.CommentLineWithDeprecation(enumDef.Deprecated)).Add(astForEnumTypeDecls(enumDef.Name))
file.Add(astForEnumValueConstants(enumDef.Name, enumDef.Values))
file.Add(astForEnumValueMethodNew(enumDef.Name))
file.Add(astForEnumValuesFunction(enumDef.Name, enumDef.Values))
file.Add(astForEnumConstructor(enumDef.Name))
file.Add(astForEnumIsUnknown(enumDef.Name, enumDef.Values))
Expand All @@ -55,6 +56,17 @@ func astForEnumValueConstants(typeName string, values []*types.Field) *jen.State
})
}

func astForEnumValueMethodNew(typeName string) *jen.Statement {
return jen.Func().
Params(jen.Id(enumReceiverName).Id(typeName + "_Value")).
Id("New").
Params().
Params(jen.Id(typeName)).
Block(
jen.Return(jen.Id(typeName).Values(jen.Id(enumStructFieldName).Op(":").Id(enumReceiverName))),
)
}

func astForEnumValuesFunction(typeName string, enumValues []*types.Field) *jen.Statement {
return jen.Commentf("%s_Values returns all known variants of %s.", typeName, typeName).
Line().
Expand Down Expand Up @@ -127,7 +139,7 @@ func astForEnumUnmarshalText(typeName string, values []*types.Field) *jen.Statem
jen.Id(enumUpperVarName),
).BlockFunc(func(cases *jen.Group) {
assign := func(val jen.Code) *jen.Statement {
return jen.Op("*").Add(jen.Id(enumReceiverName)).Op("=").Id("New_" + typeName).Call(val)
return jen.Op("*").Add(jen.Id(enumReceiverName)).Op("=").Add(val).Dot("New").Call()
}
cases.Default().Block(assign(jen.Id(typeName + "_Value").Call(jen.Id(enumUpperVarName))))
for _, valDef := range values {
Expand Down
6 changes: 3 additions & 3 deletions conjure/serverwriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -527,11 +527,11 @@ func argDefLogSafety(argDef *types.EndpointArgumentDefinition) spec.LogSafety {
if ext, ok := marker.(*types.External); ok && ext.Spec.Package == "com.palantir.logsafe" {
switch ext.Spec.Name {
case "DoNotLog":
return spec.New_LogSafety(spec.LogSafety_DO_NOT_LOG)
return spec.LogSafety_DO_NOT_LOG.New()
case "Safe":
return spec.New_LogSafety(spec.LogSafety_SAFE)
return spec.LogSafety_SAFE.New()
case "Unsafe":
return spec.New_LogSafety(spec.LogSafety_UNSAFE)
return spec.LogSafety_UNSAFE.New()
}
}
}
Expand Down
Loading