From 120c8337e8a71b51f6e8e06f5de2f1bc2009aff1 Mon Sep 17 00:00:00 2001 From: Brad Moylan Date: Thu, 7 Dec 2023 19:13:05 -0800 Subject: [PATCH 1/2] improvement: Enum values expose New() to ease creating new container types --- conjure-api/conjure/spec/enums.conjure.go | 80 +++++++++++-------- .../verification/types/enums.conjure.go | 22 +++-- .../com/palantir/buzz/enums.conjure.go | 10 ++- .../com/palantir/buzz/enums.conjure.go | 10 ++- .../com/palantir/buzz/enums.conjure.go | 10 ++- .../com/palantir/buzz/enums.conjure.go | 10 ++- .../com/palantir/buzz/enums.conjure.go | 10 ++- conjure/enumwriter.go | 14 +++- conjure/serverwriter.go | 6 +- conjure/types/conjure_definition_test.go | 30 +++---- conjure/types/types.go | 2 +- .../testgenerated/cli/api/enums.conjure.go | 10 ++- .../testgenerated/cli/cli_test.go | 2 +- .../objects/api/enums.conjure.go | 34 +++++--- .../testgenerated/objects/objects_test.go | 4 +- 15 files changed, 163 insertions(+), 91 deletions(-) diff --git a/conjure-api/conjure/spec/enums.conjure.go b/conjure-api/conjure/spec/enums.conjure.go index a3ce471e3..6275c7804 100644 --- a/conjure-api/conjure/spec/enums.conjure.go +++ b/conjure-api/conjure/spec/enums.conjure.go @@ -26,6 +26,10 @@ const ( ErrorCode_UNKNOWN ErrorCode_Value = "UNKNOWN" ) +func (e ErrorCode_Value) New() ErrorCode { + return ErrorCode{val: e} +} + // ErrorCode_Values returns all known variants of ErrorCode. func ErrorCode_Values() []ErrorCode_Value { return []ErrorCode_Value{ErrorCode_PERMISSION_DENIED, ErrorCode_INVALID_ARGUMENT, ErrorCode_NOT_FOUND, ErrorCode_CONFLICT, ErrorCode_REQUEST_ENTITY_TOO_LARGE, ErrorCode_FAILED_PRECONDITION, ErrorCode_INTERNAL, ErrorCode_TIMEOUT, ErrorCode_CUSTOM_CLIENT, ErrorCode_CUSTOM_SERVER} @@ -62,27 +66,27 @@ func (e ErrorCode) MarshalText() ([]byte, error) { func (e *ErrorCode) UnmarshalText(data []byte) error { switch v := strings.ToUpper(string(data)); v { default: - *e = New_ErrorCode(ErrorCode_Value(v)) + *e = ErrorCode_Value(v).New() case "PERMISSION_DENIED": - *e = New_ErrorCode(ErrorCode_PERMISSION_DENIED) + *e = ErrorCode_PERMISSION_DENIED.New() case "INVALID_ARGUMENT": - *e = New_ErrorCode(ErrorCode_INVALID_ARGUMENT) + *e = ErrorCode_INVALID_ARGUMENT.New() case "NOT_FOUND": - *e = New_ErrorCode(ErrorCode_NOT_FOUND) + *e = ErrorCode_NOT_FOUND.New() case "CONFLICT": - *e = New_ErrorCode(ErrorCode_CONFLICT) + *e = ErrorCode_CONFLICT.New() case "REQUEST_ENTITY_TOO_LARGE": - *e = New_ErrorCode(ErrorCode_REQUEST_ENTITY_TOO_LARGE) + *e = ErrorCode_REQUEST_ENTITY_TOO_LARGE.New() case "FAILED_PRECONDITION": - *e = New_ErrorCode(ErrorCode_FAILED_PRECONDITION) + *e = ErrorCode_FAILED_PRECONDITION.New() case "INTERNAL": - *e = New_ErrorCode(ErrorCode_INTERNAL) + *e = ErrorCode_INTERNAL.New() case "TIMEOUT": - *e = New_ErrorCode(ErrorCode_TIMEOUT) + *e = ErrorCode_TIMEOUT.New() case "CUSTOM_CLIENT": - *e = New_ErrorCode(ErrorCode_CUSTOM_CLIENT) + *e = ErrorCode_CUSTOM_CLIENT.New() case "CUSTOM_SERVER": - *e = New_ErrorCode(ErrorCode_CUSTOM_SERVER) + *e = ErrorCode_CUSTOM_SERVER.New() } return nil } @@ -101,6 +105,10 @@ const ( HttpMethod_UNKNOWN HttpMethod_Value = "UNKNOWN" ) +func (e HttpMethod_Value) New() HttpMethod { + return HttpMethod{val: e} +} + // HttpMethod_Values returns all known variants of HttpMethod. func HttpMethod_Values() []HttpMethod_Value { return []HttpMethod_Value{HttpMethod_GET, HttpMethod_POST, HttpMethod_PUT, HttpMethod_DELETE} @@ -137,15 +145,15 @@ func (e HttpMethod) MarshalText() ([]byte, error) { func (e *HttpMethod) UnmarshalText(data []byte) error { switch v := strings.ToUpper(string(data)); v { default: - *e = New_HttpMethod(HttpMethod_Value(v)) + *e = HttpMethod_Value(v).New() case "GET": - *e = New_HttpMethod(HttpMethod_GET) + *e = HttpMethod_GET.New() case "POST": - *e = New_HttpMethod(HttpMethod_POST) + *e = HttpMethod_POST.New() case "PUT": - *e = New_HttpMethod(HttpMethod_PUT) + *e = HttpMethod_PUT.New() case "DELETE": - *e = New_HttpMethod(HttpMethod_DELETE) + *e = HttpMethod_DELETE.New() } return nil } @@ -167,6 +175,10 @@ const ( LogSafety_UNKNOWN LogSafety_Value = "UNKNOWN" ) +func (e LogSafety_Value) New() LogSafety { + return LogSafety{val: e} +} + // LogSafety_Values returns all known variants of LogSafety. func LogSafety_Values() []LogSafety_Value { return []LogSafety_Value{LogSafety_SAFE, LogSafety_UNSAFE, LogSafety_DO_NOT_LOG} @@ -203,13 +215,13 @@ func (e LogSafety) MarshalText() ([]byte, error) { func (e *LogSafety) UnmarshalText(data []byte) error { switch v := strings.ToUpper(string(data)); v { default: - *e = New_LogSafety(LogSafety_Value(v)) + *e = LogSafety_Value(v).New() case "SAFE": - *e = New_LogSafety(LogSafety_SAFE) + *e = LogSafety_SAFE.New() case "UNSAFE": - *e = New_LogSafety(LogSafety_UNSAFE) + *e = LogSafety_UNSAFE.New() case "DO_NOT_LOG": - *e = New_LogSafety(LogSafety_DO_NOT_LOG) + *e = LogSafety_DO_NOT_LOG.New() } return nil } @@ -235,6 +247,10 @@ const ( PrimitiveType_UNKNOWN PrimitiveType_Value = "UNKNOWN" ) +func (e PrimitiveType_Value) New() PrimitiveType { + return PrimitiveType{val: e} +} + // PrimitiveType_Values returns all known variants of PrimitiveType. func PrimitiveType_Values() []PrimitiveType_Value { return []PrimitiveType_Value{PrimitiveType_STRING, PrimitiveType_DATETIME, PrimitiveType_INTEGER, PrimitiveType_DOUBLE, PrimitiveType_SAFELONG, PrimitiveType_BINARY, PrimitiveType_ANY, PrimitiveType_BOOLEAN, PrimitiveType_UUID, PrimitiveType_RID, PrimitiveType_BEARERTOKEN} @@ -271,29 +287,29 @@ func (e PrimitiveType) MarshalText() ([]byte, error) { func (e *PrimitiveType) UnmarshalText(data []byte) error { switch v := strings.ToUpper(string(data)); v { default: - *e = New_PrimitiveType(PrimitiveType_Value(v)) + *e = PrimitiveType_Value(v).New() case "STRING": - *e = New_PrimitiveType(PrimitiveType_STRING) + *e = PrimitiveType_STRING.New() case "DATETIME": - *e = New_PrimitiveType(PrimitiveType_DATETIME) + *e = PrimitiveType_DATETIME.New() case "INTEGER": - *e = New_PrimitiveType(PrimitiveType_INTEGER) + *e = PrimitiveType_INTEGER.New() case "DOUBLE": - *e = New_PrimitiveType(PrimitiveType_DOUBLE) + *e = PrimitiveType_DOUBLE.New() case "SAFELONG": - *e = New_PrimitiveType(PrimitiveType_SAFELONG) + *e = PrimitiveType_SAFELONG.New() case "BINARY": - *e = New_PrimitiveType(PrimitiveType_BINARY) + *e = PrimitiveType_BINARY.New() case "ANY": - *e = New_PrimitiveType(PrimitiveType_ANY) + *e = PrimitiveType_ANY.New() case "BOOLEAN": - *e = New_PrimitiveType(PrimitiveType_BOOLEAN) + *e = PrimitiveType_BOOLEAN.New() case "UUID": - *e = New_PrimitiveType(PrimitiveType_UUID) + *e = PrimitiveType_UUID.New() case "RID": - *e = New_PrimitiveType(PrimitiveType_RID) + *e = PrimitiveType_RID.New() case "BEARERTOKEN": - *e = New_PrimitiveType(PrimitiveType_BEARERTOKEN) + *e = PrimitiveType_BEARERTOKEN.New() } return nil } diff --git a/conjure-go-verifier/conjure/verification/types/enums.conjure.go b/conjure-go-verifier/conjure/verification/types/enums.conjure.go index 93c8db5dc..e8d7048cc 100644 --- a/conjure-go-verifier/conjure/verification/types/enums.conjure.go +++ b/conjure-go-verifier/conjure/verification/types/enums.conjure.go @@ -18,6 +18,10 @@ const ( Enum_UNKNOWN Enum_Value = "UNKNOWN" ) +func (e Enum_Value) New() Enum { + return Enum{val: e} +} + // Enum_Values returns all known variants of Enum. func Enum_Values() []Enum_Value { return []Enum_Value{Enum_ONE, Enum_TWO} @@ -54,11 +58,11 @@ func (e Enum) MarshalText() ([]byte, error) { func (e *Enum) UnmarshalText(data []byte) error { switch v := strings.ToUpper(string(data)); v { default: - *e = New_Enum(Enum_Value(v)) + *e = Enum_Value(v).New() case "ONE": - *e = New_Enum(Enum_ONE) + *e = Enum_ONE.New() case "TWO": - *e = New_Enum(Enum_TWO) + *e = Enum_TWO.New() } return nil } @@ -76,6 +80,10 @@ const ( EnumExample_UNKNOWN EnumExample_Value = "UNKNOWN" ) +func (e EnumExample_Value) New() EnumExample { + return EnumExample{val: e} +} + // EnumExample_Values returns all known variants of EnumExample. func EnumExample_Values() []EnumExample_Value { return []EnumExample_Value{EnumExample_ONE, EnumExample_TWO, EnumExample_ONE_HUNDRED} @@ -112,13 +120,13 @@ func (e EnumExample) MarshalText() ([]byte, error) { func (e *EnumExample) UnmarshalText(data []byte) error { switch v := strings.ToUpper(string(data)); v { default: - *e = New_EnumExample(EnumExample_Value(v)) + *e = EnumExample_Value(v).New() case "ONE": - *e = New_EnumExample(EnumExample_ONE) + *e = EnumExample_ONE.New() case "TWO": - *e = New_EnumExample(EnumExample_TWO) + *e = EnumExample_TWO.New() case "ONE_HUNDRED": - *e = New_EnumExample(EnumExample_ONE_HUNDRED) + *e = EnumExample_ONE_HUNDRED.New() } return nil } diff --git a/conjure/cycles/testdata/cycle-within-pkg/conjure/com/palantir/buzz/enums.conjure.go b/conjure/cycles/testdata/cycle-within-pkg/conjure/com/palantir/buzz/enums.conjure.go index 9453af6dd..bc68a8f94 100644 --- a/conjure/cycles/testdata/cycle-within-pkg/conjure/com/palantir/buzz/enums.conjure.go +++ b/conjure/cycles/testdata/cycle-within-pkg/conjure/com/palantir/buzz/enums.conjure.go @@ -18,6 +18,10 @@ const ( Type1_UNKNOWN Type1_Value = "UNKNOWN" ) +func (e Type1_Value) New() Type1 { + return Type1{val: e} +} + // Type1_Values returns all known variants of Type1. func Type1_Values() []Type1_Value { return []Type1_Value{Type1_value1, Type1_value2} @@ -54,11 +58,11 @@ func (e Type1) MarshalText() ([]byte, error) { func (e *Type1) UnmarshalText(data []byte) error { switch v := strings.ToUpper(string(data)); v { default: - *e = New_Type1(Type1_Value(v)) + *e = Type1_Value(v).New() case "value1": - *e = New_Type1(Type1_value1) + *e = Type1_value1.New() case "value2": - *e = New_Type1(Type1_value2) + *e = Type1_value2.New() } return nil } diff --git a/conjure/cycles/testdata/no-cycles/conjure/com/palantir/buzz/enums.conjure.go b/conjure/cycles/testdata/no-cycles/conjure/com/palantir/buzz/enums.conjure.go index 9453af6dd..bc68a8f94 100644 --- a/conjure/cycles/testdata/no-cycles/conjure/com/palantir/buzz/enums.conjure.go +++ b/conjure/cycles/testdata/no-cycles/conjure/com/palantir/buzz/enums.conjure.go @@ -18,6 +18,10 @@ const ( Type1_UNKNOWN Type1_Value = "UNKNOWN" ) +func (e Type1_Value) New() Type1 { + return Type1{val: e} +} + // Type1_Values returns all known variants of Type1. func Type1_Values() []Type1_Value { return []Type1_Value{Type1_value1, Type1_value2} @@ -54,11 +58,11 @@ func (e Type1) MarshalText() ([]byte, error) { func (e *Type1) UnmarshalText(data []byte) error { switch v := strings.ToUpper(string(data)); v { default: - *e = New_Type1(Type1_Value(v)) + *e = Type1_Value(v).New() case "value1": - *e = New_Type1(Type1_value1) + *e = Type1_value1.New() case "value2": - *e = New_Type1(Type1_value2) + *e = Type1_value2.New() } return nil } diff --git a/conjure/cycles/testdata/pkg-cycle-disconnected/conjure/com/palantir/buzz/enums.conjure.go b/conjure/cycles/testdata/pkg-cycle-disconnected/conjure/com/palantir/buzz/enums.conjure.go index 9453af6dd..bc68a8f94 100644 --- a/conjure/cycles/testdata/pkg-cycle-disconnected/conjure/com/palantir/buzz/enums.conjure.go +++ b/conjure/cycles/testdata/pkg-cycle-disconnected/conjure/com/palantir/buzz/enums.conjure.go @@ -18,6 +18,10 @@ const ( Type1_UNKNOWN Type1_Value = "UNKNOWN" ) +func (e Type1_Value) New() Type1 { + return Type1{val: e} +} + // Type1_Values returns all known variants of Type1. func Type1_Values() []Type1_Value { return []Type1_Value{Type1_value1, Type1_value2} @@ -54,11 +58,11 @@ func (e Type1) MarshalText() ([]byte, error) { func (e *Type1) UnmarshalText(data []byte) error { switch v := strings.ToUpper(string(data)); v { default: - *e = New_Type1(Type1_Value(v)) + *e = Type1_Value(v).New() case "value1": - *e = New_Type1(Type1_value1) + *e = Type1_value1.New() case "value2": - *e = New_Type1(Type1_value2) + *e = Type1_value2.New() } return nil } diff --git a/conjure/cycles/testdata/pkg-cycle/conjure/com/palantir/buzz/enums.conjure.go b/conjure/cycles/testdata/pkg-cycle/conjure/com/palantir/buzz/enums.conjure.go index 9453af6dd..bc68a8f94 100644 --- a/conjure/cycles/testdata/pkg-cycle/conjure/com/palantir/buzz/enums.conjure.go +++ b/conjure/cycles/testdata/pkg-cycle/conjure/com/palantir/buzz/enums.conjure.go @@ -18,6 +18,10 @@ const ( Type1_UNKNOWN Type1_Value = "UNKNOWN" ) +func (e Type1_Value) New() Type1 { + return Type1{val: e} +} + // Type1_Values returns all known variants of Type1. func Type1_Values() []Type1_Value { return []Type1_Value{Type1_value1, Type1_value2} @@ -54,11 +58,11 @@ func (e Type1) MarshalText() ([]byte, error) { func (e *Type1) UnmarshalText(data []byte) error { switch v := strings.ToUpper(string(data)); v { default: - *e = New_Type1(Type1_Value(v)) + *e = Type1_Value(v).New() case "value1": - *e = New_Type1(Type1_value1) + *e = Type1_value1.New() case "value2": - *e = New_Type1(Type1_value2) + *e = Type1_value2.New() } return nil } diff --git a/conjure/cycles/testdata/type-cycle/conjure/com/palantir/buzz/enums.conjure.go b/conjure/cycles/testdata/type-cycle/conjure/com/palantir/buzz/enums.conjure.go index 9453af6dd..bc68a8f94 100644 --- a/conjure/cycles/testdata/type-cycle/conjure/com/palantir/buzz/enums.conjure.go +++ b/conjure/cycles/testdata/type-cycle/conjure/com/palantir/buzz/enums.conjure.go @@ -18,6 +18,10 @@ const ( Type1_UNKNOWN Type1_Value = "UNKNOWN" ) +func (e Type1_Value) New() Type1 { + return Type1{val: e} +} + // Type1_Values returns all known variants of Type1. func Type1_Values() []Type1_Value { return []Type1_Value{Type1_value1, Type1_value2} @@ -54,11 +58,11 @@ func (e Type1) MarshalText() ([]byte, error) { func (e *Type1) UnmarshalText(data []byte) error { switch v := strings.ToUpper(string(data)); v { default: - *e = New_Type1(Type1_Value(v)) + *e = Type1_Value(v).New() case "value1": - *e = New_Type1(Type1_value1) + *e = Type1_value1.New() case "value2": - *e = New_Type1(Type1_value2) + *e = Type1_value2.New() } return nil } diff --git a/conjure/enumwriter.go b/conjure/enumwriter.go index 8b6b9f188..22622a84f 100644 --- a/conjure/enumwriter.go +++ b/conjure/enumwriter.go @@ -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)) @@ -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(). @@ -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 { diff --git a/conjure/serverwriter.go b/conjure/serverwriter.go index 4041e5946..36ceaaf80 100644 --- a/conjure/serverwriter.go +++ b/conjure/serverwriter.go @@ -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() } } } diff --git a/conjure/types/conjure_definition_test.go b/conjure/types/conjure_definition_test.go index a57a72509..de8791c1c 100644 --- a/conjure/types/conjure_definition_test.go +++ b/conjure/types/conjure_definition_test.go @@ -417,7 +417,7 @@ func TestNewConjureDefinition(t *testing.T) { Endpoints: []spec.EndpointDefinition{ { EndpointName: "getFileSystems", - HttpMethod: spec.New_HttpMethod(spec.HttpMethod_GET), + HttpMethod: spec.HttpMethod_GET.New(), HttpPath: "/catalog/fileSystems", Auth: authPtr(spec.NewAuthTypeFromHeader(spec.HeaderAuthType{})), Returns: specTypePtr(spec.NewTypeFromMap(spec.MapType{ @@ -428,7 +428,7 @@ func TestNewConjureDefinition(t *testing.T) { }, { EndpointName: "createDataset", - HttpMethod: spec.New_HttpMethod(spec.HttpMethod_POST), + HttpMethod: spec.HttpMethod_POST.New(), HttpPath: "/catalog/datasets", Auth: authPtr(spec.NewAuthTypeFromCookie(spec.CookieAuthType{CookieName: "PALANTIR_TOKEN"})), Args: []spec.ArgumentDefinition{ @@ -441,14 +441,14 @@ func TestNewConjureDefinition(t *testing.T) { }, { EndpointName: "streamResponse", - HttpMethod: spec.New_HttpMethod(spec.HttpMethod_GET), + HttpMethod: spec.HttpMethod_GET.New(), HttpPath: "/catalog/streamResponse", Auth: authPtr(spec.NewAuthTypeFromHeader(spec.HeaderAuthType{})), Returns: specTypePtr(newPrimitive(spec.PrimitiveType_BINARY)), }, { EndpointName: "maybeStreamResponse", - HttpMethod: spec.New_HttpMethod(spec.HttpMethod_GET), + HttpMethod: spec.HttpMethod_GET.New(), HttpPath: "/catalog/maybe/streamResponse", Auth: authPtr(spec.NewAuthTypeFromHeader(spec.HeaderAuthType{})), Returns: specTypePtr(spec.NewTypeFromOptional(spec.OptionalType{ @@ -457,7 +457,7 @@ func TestNewConjureDefinition(t *testing.T) { }, { EndpointName: "queryParams", - HttpMethod: spec.New_HttpMethod(spec.HttpMethod_GET), + HttpMethod: spec.HttpMethod_GET.New(), HttpPath: "/catalog/echo", Args: []spec.ArgumentDefinition{ { @@ -492,14 +492,14 @@ func TestNewConjureDefinition(t *testing.T) { { Docs: "Returns a mapping from file system id to backing file system configuration.", EndpointName: "getFileSystems", - HTTPMethod: spec.New_HttpMethod(spec.HttpMethod_GET), + HTTPMethod: spec.HttpMethod_GET.New(), HTTPPath: "/catalog/fileSystems", HeaderAuth: true, Returns: typePtr(&Map{Key: String{}, Val: Integer{}}), }, { EndpointName: "createDataset", - HTTPMethod: spec.New_HttpMethod(spec.HttpMethod_POST), + HTTPMethod: spec.HttpMethod_POST.New(), HTTPPath: "/catalog/datasets", CookieAuth: stringPtr("PALANTIR_TOKEN"), Params: []*EndpointArgumentDefinition{ @@ -513,21 +513,21 @@ func TestNewConjureDefinition(t *testing.T) { }, { EndpointName: "streamResponse", - HTTPMethod: spec.New_HttpMethod(spec.HttpMethod_GET), + HTTPMethod: spec.HttpMethod_GET.New(), HTTPPath: "/catalog/streamResponse", HeaderAuth: true, Returns: typePtr(Binary{}), }, { EndpointName: "maybeStreamResponse", - HTTPMethod: spec.New_HttpMethod(spec.HttpMethod_GET), + HTTPMethod: spec.HttpMethod_GET.New(), HTTPPath: "/catalog/maybe/streamResponse", HeaderAuth: true, Returns: typePtr(&Optional{Item: Binary{}}), }, { EndpointName: "queryParams", - HTTPMethod: spec.New_HttpMethod(spec.HttpMethod_GET), + HTTPMethod: spec.HttpMethod_GET.New(), HTTPPath: "/catalog/echo", Params: []*EndpointArgumentDefinition{ { @@ -586,7 +586,7 @@ func TestNewConjureDefinition(t *testing.T) { Endpoints: []spec.EndpointDefinition{ { EndpointName: "getFileSystems", - HttpMethod: spec.New_HttpMethod(spec.HttpMethod_GET), + HttpMethod: spec.HttpMethod_GET.New(), HttpPath: "/catalog/fileSystems", Auth: authPtr(spec.NewAuthTypeFromHeader(spec.HeaderAuthType{})), Returns: specTypePtr(spec.NewTypeFromMap(spec.MapType{ @@ -644,7 +644,7 @@ func TestNewConjureDefinition(t *testing.T) { { Docs: "Returns a mapping from file system id to backing file system configuration.", EndpointName: "getFileSystems", - HTTPMethod: spec.New_HttpMethod(spec.HttpMethod_GET), + HTTPMethod: spec.HttpMethod_GET.New(), HTTPPath: "/catalog/fileSystems", HeaderAuth: true, Returns: typePtr(&Map{ @@ -727,7 +727,7 @@ func TestNewConjureDefinition(t *testing.T) { }, Docs: docsPtr("This is documentation of MyNotFound error."), Namespace: "MyNamespace", - Code: spec.New_ErrorCode(spec.ErrorCode_NOT_FOUND), + Code: spec.ErrorCode_NOT_FOUND.New(), SafeArgs: []spec.FieldDefinition{ { FieldName: "safeArgA", @@ -787,7 +787,7 @@ func TestNewConjureDefinition(t *testing.T) { Docs: "This is documentation of MyNotFound error.", Name: "MyNotFound", ErrorNamespace: "MyNamespace", - ErrorCode: spec.New_ErrorCode(spec.ErrorCode_NOT_FOUND), + ErrorCode: spec.ErrorCode_NOT_FOUND.New(), SafeArgs: []*Field{ { Docs: "This is safeArgA doc.", @@ -950,7 +950,7 @@ func TestSanitizePackageName(t *testing.T) { } func newPrimitive(kind spec.PrimitiveType_Value) spec.Type { - return spec.NewTypeFromPrimitive(spec.New_PrimitiveType(kind)) + return spec.NewTypeFromPrimitive(kind.New()) } func stringPtr(s string) *string { return &s } diff --git a/conjure/types/types.go b/conjure/types/types.go index 3bd1c035e..3dfeb9afa 100644 --- a/conjure/types/types.go +++ b/conjure/types/types.go @@ -382,5 +382,5 @@ func (base) IsOptional() bool { return false } func (base) IsCollection() bool { return false } func (base) IsList() bool { return false } func (base) ContainsStrictFields() bool { return false } -func (base) Safety() spec.LogSafety { return spec.New_LogSafety(spec.LogSafety_UNKNOWN) } +func (base) Safety() spec.LogSafety { return spec.LogSafety_UNKNOWN.New() } func (base) typ() {} diff --git a/integration_test/testgenerated/cli/api/enums.conjure.go b/integration_test/testgenerated/cli/api/enums.conjure.go index 56f7b8e8d..9bfa9a5c7 100644 --- a/integration_test/testgenerated/cli/api/enums.conjure.go +++ b/integration_test/testgenerated/cli/api/enums.conjure.go @@ -18,6 +18,10 @@ const ( CustomEnum_UNKNOWN CustomEnum_Value = "UNKNOWN" ) +func (e CustomEnum_Value) New() CustomEnum { + return CustomEnum{val: e} +} + // CustomEnum_Values returns all known variants of CustomEnum. func CustomEnum_Values() []CustomEnum_Value { return []CustomEnum_Value{CustomEnum_STATE1, CustomEnum_STATE2} @@ -54,11 +58,11 @@ func (e CustomEnum) MarshalText() ([]byte, error) { func (e *CustomEnum) UnmarshalText(data []byte) error { switch v := strings.ToUpper(string(data)); v { default: - *e = New_CustomEnum(CustomEnum_Value(v)) + *e = CustomEnum_Value(v).New() case "STATE1": - *e = New_CustomEnum(CustomEnum_STATE1) + *e = CustomEnum_STATE1.New() case "STATE2": - *e = New_CustomEnum(CustomEnum_STATE2) + *e = CustomEnum_STATE2.New() } return nil } diff --git a/integration_test/testgenerated/cli/cli_test.go b/integration_test/testgenerated/cli/cli_test.go index a6d82d716..78e357e3a 100644 --- a/integration_test/testgenerated/cli/cli_test.go +++ b/integration_test/testgenerated/cli/cli_test.go @@ -718,7 +718,7 @@ func TestCommand_GetUuid(t *testing.T) { } func TestCommand_GetCustomEnum(t *testing.T) { - customEnum := api.New_CustomEnum(api.CustomEnum_STATE1) + customEnum := api.CustomEnum_STATE1.New() t.Run("valid input", func(t *testing.T) { args := []string{ "", diff --git a/integration_test/testgenerated/objects/api/enums.conjure.go b/integration_test/testgenerated/objects/api/enums.conjure.go index 1d9ef787c..921d7ad0e 100644 --- a/integration_test/testgenerated/objects/api/enums.conjure.go +++ b/integration_test/testgenerated/objects/api/enums.conjure.go @@ -18,6 +18,10 @@ const ( Days_UNKNOWN Days_Value = "UNKNOWN" ) +func (e Days_Value) New() Days { + return Days{val: e} +} + // Days_Values returns all known variants of Days. func Days_Values() []Days_Value { return []Days_Value{Days_FRIDAY, Days_SATURDAY} @@ -54,11 +58,11 @@ func (e Days) MarshalText() ([]byte, error) { func (e *Days) UnmarshalText(data []byte) error { switch v := strings.ToUpper(string(data)); v { default: - *e = New_Days(Days_Value(v)) + *e = Days_Value(v).New() case "FRIDAY": - *e = New_Days(Days_FRIDAY) + *e = Days_FRIDAY.New() case "SATURDAY": - *e = New_Days(Days_SATURDAY) + *e = Days_SATURDAY.New() } return nil } @@ -73,6 +77,10 @@ const ( EmptyValuesEnum_UNKNOWN EmptyValuesEnum_Value = "UNKNOWN" ) +func (e EmptyValuesEnum_Value) New() EmptyValuesEnum { + return EmptyValuesEnum{val: e} +} + // EmptyValuesEnum_Values returns all known variants of EmptyValuesEnum. func EmptyValuesEnum_Values() []EmptyValuesEnum_Value { return []EmptyValuesEnum_Value{} @@ -105,7 +113,7 @@ func (e EmptyValuesEnum) MarshalText() ([]byte, error) { func (e *EmptyValuesEnum) UnmarshalText(data []byte) error { switch v := strings.ToUpper(string(data)); v { default: - *e = New_EmptyValuesEnum(EmptyValuesEnum_Value(v)) + *e = EmptyValuesEnum_Value(v).New() } return nil } @@ -132,6 +140,10 @@ const ( Enum_UNKNOWN Enum_Value = "UNKNOWN" ) +func (e Enum_Value) New() Enum { + return Enum{val: e} +} + // Enum_Values returns all known variants of Enum. func Enum_Values() []Enum_Value { return []Enum_Value{Enum_VALUE, Enum_VALUES, Enum_VALUES_1, Enum_VALUES_1_1, Enum_VALUE1, Enum_VALUE2} @@ -168,19 +180,19 @@ func (e Enum) MarshalText() ([]byte, error) { func (e *Enum) UnmarshalText(data []byte) error { switch v := strings.ToUpper(string(data)); v { default: - *e = New_Enum(Enum_Value(v)) + *e = Enum_Value(v).New() case "VALUE": - *e = New_Enum(Enum_VALUE) + *e = Enum_VALUE.New() case "VALUES": - *e = New_Enum(Enum_VALUES) + *e = Enum_VALUES.New() case "VALUES_1": - *e = New_Enum(Enum_VALUES_1) + *e = Enum_VALUES_1.New() case "VALUES_1_1": - *e = New_Enum(Enum_VALUES_1_1) + *e = Enum_VALUES_1_1.New() case "VALUE1": - *e = New_Enum(Enum_VALUE1) + *e = Enum_VALUE1.New() case "VALUE2": - *e = New_Enum(Enum_VALUE2) + *e = Enum_VALUE2.New() } return nil } diff --git a/integration_test/testgenerated/objects/objects_test.go b/integration_test/testgenerated/objects/objects_test.go index 7170568a7..e439fe4ec 100644 --- a/integration_test/testgenerated/objects/objects_test.go +++ b/integration_test/testgenerated/objects/objects_test.go @@ -408,7 +408,7 @@ func TestEnum(t *testing.T) { } func TestEnumIsUnknown(t *testing.T) { - assert.False(t, api.New_Enum(api.Enum_VALUE1).IsUnknown()) + assert.False(t, api.Enum_VALUE1.New().IsUnknown()) assert.True(t, api.New_Enum("OTHER").IsUnknown()) } @@ -424,5 +424,5 @@ func TestEnumValues(t *testing.T) { func TestEmptyValuesEnumIsAlwaysUnknown(t *testing.T) { assert.True(t, api.New_EmptyValuesEnum("test-value").IsUnknown()) - assert.True(t, api.New_EmptyValuesEnum(api.EmptyValuesEnum_UNKNOWN).IsUnknown()) + assert.True(t, api.EmptyValuesEnum_UNKNOWN.New().IsUnknown()) } From 74c7413cd5665cd4dab519680ba580779b1a4513 Mon Sep 17 00:00:00 2001 From: svc-changelog Date: Fri, 8 Dec 2023 03:13:28 +0000 Subject: [PATCH 2/2] Add generated changelog entries --- changelog/@unreleased/pr-542.v2.yml | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 changelog/@unreleased/pr-542.v2.yml diff --git a/changelog/@unreleased/pr-542.v2.yml b/changelog/@unreleased/pr-542.v2.yml new file mode 100644 index 000000000..60ac26acf --- /dev/null +++ b/changelog/@unreleased/pr-542.v2.yml @@ -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