diff --git a/pkg/http/annotations.go b/pkg/annotations/annotations.go similarity index 64% rename from pkg/http/annotations.go rename to pkg/annotations/annotations.go index 28373d5..6164a62 100644 --- a/pkg/http/annotations.go +++ b/pkg/annotations/annotations.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package http +package annotations import ( "fmt" @@ -22,9 +22,9 @@ import ( "github.com/openshift-online/ocm-api-metamodel/pkg/concepts" ) -// httpName checks if the given concept has a `http` annotation. If it does then it returns the +// HTTPName checks if the given concept has a `http` annotation. If it does then it returns the // value of the `name` parameter. If it doesn't, it returns an empty string. -func httpName(concept concepts.Annotated) string { +func HTTPName(concept concepts.Annotated) string { annotation := concept.GetAnnotation("http") if annotation == nil { return "" @@ -36,9 +36,9 @@ func httpName(concept concepts.Annotated) string { return fmt.Sprintf("%s", name) } -// jsonName checks if the given concept has a `json` annotation. If it does then it returns the +// JSONName checks if the given concept has a `json` annotation. If it does then it returns the // value of the `name` parameter. If it doesn't, it returns an empty string. -func jsonName(concept concepts.Annotated) string { +func JSONName(concept concepts.Annotated) string { annotation := concept.GetAnnotation("json") if annotation == nil { return "" @@ -49,3 +49,17 @@ func jsonName(concept concepts.Annotated) string { } return fmt.Sprintf("%s", name) } + +// GoName checks if the given concept as a `go` annotation. If it has it then it returns the value +// of the `name` parameter. It returns an empty string if there is no such annotation or parameter. +func GoName(concept concepts.Annotated) string { + annotation := concept.GetAnnotation("go") + if annotation == nil { + return "" + } + name := annotation.FindParameter("name") + if name == nil { + return "" + } + return fmt.Sprintf("%s", name) +} diff --git a/pkg/generators/golang/annotations.go b/pkg/generators/golang/annotations.go deleted file mode 100644 index fc7f865..0000000 --- a/pkg/generators/golang/annotations.go +++ /dev/null @@ -1,37 +0,0 @@ -/* -Copyright (c) 2022 Red Hat, Inc. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package golang - -import ( - "fmt" - - "github.com/openshift-online/ocm-api-metamodel/pkg/concepts" -) - -// goName checks if the given concept as a `go` annotation. If it has it then it returns the value -// of the `name` parameter. It returns an empty string if there is no such annotation or parameter. -func goName(concept concepts.Annotated) string { - annotation := concept.GetAnnotation("go") - if annotation == nil { - return "" - } - name := annotation.FindParameter("name") - if name == nil { - return "" - } - return fmt.Sprintf("%s", name) -} diff --git a/pkg/generators/golang/builders_generator.go b/pkg/generators/golang/builders_generator.go index e4d64f2..ebec888 100644 --- a/pkg/generators/golang/builders_generator.go +++ b/pkg/generators/golang/builders_generator.go @@ -19,6 +19,7 @@ package golang import ( "fmt" + "github.com/openshift-online/ocm-api-metamodel/pkg/annotations" "github.com/openshift-online/ocm-api-metamodel/pkg/concepts" "github.com/openshift-online/ocm-api-metamodel/pkg/names" "github.com/openshift-online/ocm-api-metamodel/pkg/nomenclator" @@ -558,13 +559,13 @@ func (g *BuildersGenerator) objectName(typ *concepts.Type) string { var name string switch { case typ.IsStruct(): - name = goName(typ) + name = annotations.GoName(typ) if name == "" { name = g.names.Public(typ.Name()) } case typ.IsList(): element := typ.Element() - name = goName(element) + name = annotations.GoName(element) if name == "" { name = g.names.Public(element.Name()) } @@ -582,14 +583,14 @@ func (g *BuildersGenerator) builderName(typ *concepts.Type) string { var name string switch { case typ.IsStruct(): - name = goName(typ) + name = annotations.GoName(typ) if name == "" { name = g.names.Public(typ.Name()) } name += "Builder" case typ.IsList(): element := typ.Element() - name = goName(element) + name = annotations.GoName(element) if name == "" { name = g.names.Public(element.Name()) } @@ -610,13 +611,13 @@ func (g *BuildersGenerator) builderCtor(typ *concepts.Type) string { switch { case typ.IsList(): element := typ.Element() - name = goName(element) + name = annotations.GoName(element) if name == "" { name = g.names.Public(element.Name()) } name += "List" case typ.IsStruct(): - name = goName(typ) + name = annotations.GoName(typ) if name == "" { name = g.names.Public(typ.Name()) } @@ -685,7 +686,7 @@ func (g *BuildersGenerator) fieldType(attribute *concepts.Attribute) *TypeRefere } func (g *BuildersGenerator) setterName(attribute *concepts.Attribute) string { - name := goName(attribute) + name := annotations.GoName(attribute) if name == "" { name = g.names.Public(attribute.Name()) } diff --git a/pkg/generators/golang/clients_generator.go b/pkg/generators/golang/clients_generator.go index 6600cbe..81aa6eb 100644 --- a/pkg/generators/golang/clients_generator.go +++ b/pkg/generators/golang/clients_generator.go @@ -19,6 +19,7 @@ package golang import ( "fmt" + "github.com/openshift-online/ocm-api-metamodel/pkg/annotations" "github.com/openshift-online/ocm-api-metamodel/pkg/concepts" "github.com/openshift-online/ocm-api-metamodel/pkg/http" "github.com/openshift-online/ocm-api-metamodel/pkg/names" @@ -1008,7 +1009,7 @@ func (g *ClientsGenerator) generateResponseSource(method *concepts.Method) { } func (g *ClientsGenerator) versionName(version *concepts.Version) string { - name := goName(version) + name := annotations.GoName(version) if name == "" { name = g.names.Public(version.Name()) } @@ -1024,7 +1025,7 @@ func (g *ClientsGenerator) resourceFile(resource *concepts.Resource) string { } func (g *ClientsGenerator) enumName(typ *concepts.Type) string { - name := goName(typ) + name := annotations.GoName(typ) if name == "" { name = g.names.Public(typ.Name()) } @@ -1056,7 +1057,7 @@ func (g *ClientsGenerator) fieldType(parameter *concepts.Parameter) *TypeReferen } func (g *ClientsGenerator) getterName(parameter *concepts.Parameter) string { - name := goName(parameter) + name := annotations.GoName(parameter) if name == "" { name = g.names.Public(parameter.Name()) } @@ -1069,7 +1070,7 @@ func (g *ClientsGenerator) getterType(parameter *concepts.Parameter) *TypeRefere } func (g *ClientsGenerator) setterName(parameter *concepts.Parameter) string { - name := goName(parameter) + name := annotations.GoName(parameter) if name == "" { name = g.names.Public(parameter.Name()) } @@ -1102,7 +1103,7 @@ func (g *ClientsGenerator) accessorType(parameter *concepts.Parameter) *TypeRefe } func (g *ClientsGenerator) locatorName(locator *concepts.Locator) string { - name := goName(locator) + name := annotations.GoName(locator) if name == "" { name = g.names.Public(locator.Name()) } @@ -1110,7 +1111,7 @@ func (g *ClientsGenerator) locatorName(locator *concepts.Locator) string { } func (g *ClientsGenerator) methodName(method *concepts.Method) string { - name := goName(method) + name := annotations.GoName(method) if name == "" { name = g.names.Public(method.Name()) } @@ -1120,7 +1121,7 @@ func (g *ClientsGenerator) methodName(method *concepts.Method) string { func (g *ClientsGenerator) clientName(resource *concepts.Resource) string { var name string if !resource.IsRoot() { - name = goName(resource) + name = annotations.GoName(resource) if name == "" { name = g.names.Public(resource.Name()) } @@ -1133,16 +1134,16 @@ func (g *ClientsGenerator) requestName(method *concepts.Method) string { resource := method.Owner() var name string if resource.IsRoot() { - name = goName(method) + name = annotations.GoName(method) if name == "" { name = g.names.Public(method.Name()) } } else { - resourceName := goName(resource) + resourceName := annotations.GoName(resource) if resourceName == "" { resourceName = g.names.Public(resource.Name()) } - methodName := goName(method) + methodName := annotations.GoName(method) if methodName == "" { methodName = g.names.Public(method.Name()) } @@ -1156,16 +1157,16 @@ func (g *ClientsGenerator) responseName(method *concepts.Method) string { resource := method.Owner() var name string if resource.IsRoot() { - name = goName(method) + name = annotations.GoName(method) if name == "" { name = g.names.Public(method.Name()) } } else { - resourceName := goName(resource) + resourceName := annotations.GoName(resource) if resourceName == "" { resourceName = g.names.Public(resource.Name()) } - methodName := goName(method) + methodName := annotations.GoName(method) if methodName == "" { methodName = g.names.Public(method.Name()) } @@ -1176,7 +1177,7 @@ func (g *ClientsGenerator) responseName(method *concepts.Method) string { } func (g *ClientsGenerator) pollRequestName(resource *concepts.Resource) string { - name := goName(resource) + name := annotations.GoName(resource) if name == "" { name = g.names.Public(resource.Name()) } @@ -1185,7 +1186,7 @@ func (g *ClientsGenerator) pollRequestName(resource *concepts.Resource) string { } func (g *ClientsGenerator) pollResponseName(resource *concepts.Resource) string { - name := goName(resource) + name := annotations.GoName(resource) if name == "" { name = g.names.Public(resource.Name()) } diff --git a/pkg/generators/golang/json_generator.go b/pkg/generators/golang/json_generator.go index bcdba5e..de3ec23 100644 --- a/pkg/generators/golang/json_generator.go +++ b/pkg/generators/golang/json_generator.go @@ -20,6 +20,7 @@ import ( "fmt" "strconv" + "github.com/openshift-online/ocm-api-metamodel/pkg/annotations" "github.com/openshift-online/ocm-api-metamodel/pkg/concepts" "github.com/openshift-online/ocm-api-metamodel/pkg/http" "github.com/openshift-online/ocm-api-metamodel/pkg/names" @@ -1446,7 +1447,7 @@ func (g *JSONSupportGenerator) resourceFile(resource *concepts.Resource) string } func (g *JSONSupportGenerator) marshalTypeFunc(typ *concepts.Type) string { - name := goName(typ) + name := annotations.GoName(typ) if name == "" { name = g.names.Public(typ.Name()) } @@ -1460,7 +1461,7 @@ func (g *JSONSupportGenerator) writeTypeFunc(typ *concepts.Type) string { } func (g *JSONSupportGenerator) unmarshalTypeFunc(typ *concepts.Type) string { - name := goName(typ) + name := annotations.GoName(typ) if name == "" { name = g.names.Public(typ.Name()) } @@ -1485,16 +1486,16 @@ func (g *JSONSupportGenerator) clientRequestName(method *concepts.Method) string resource := method.Owner() var name string if resource.IsRoot() { - name = goName(method) + name = annotations.GoName(method) if name == "" { name = g.names.Public(method.Name()) } } else { - resourceName := goName(resource) + resourceName := annotations.GoName(resource) if resourceName == "" { resourceName = g.names.Public(resource.Name()) } - methodName := goName(method) + methodName := annotations.GoName(method) if methodName == "" { methodName = g.names.Public(method.Name()) } @@ -1508,16 +1509,16 @@ func (g *JSONSupportGenerator) clientResponseName(method *concepts.Method) strin resource := method.Owner() var name string if resource.IsRoot() { - name = goName(method) + name = annotations.GoName(method) if name == "" { name = g.names.Public(method.Name()) } } else { - resourceName := goName(resource) + resourceName := annotations.GoName(resource) if resourceName == "" { resourceName = g.names.Public(resource.Name()) } - methodName := goName(method) + methodName := annotations.GoName(method) if methodName == "" { methodName = g.names.Public(method.Name()) } diff --git a/pkg/generators/golang/types_calculator.go b/pkg/generators/golang/types_calculator.go index 7b00334..7b0fdcb 100644 --- a/pkg/generators/golang/types_calculator.go +++ b/pkg/generators/golang/types_calculator.go @@ -20,6 +20,7 @@ import ( "fmt" "path" + "github.com/openshift-online/ocm-api-metamodel/pkg/annotations" "github.com/openshift-online/ocm-api-metamodel/pkg/concepts" "github.com/openshift-online/ocm-api-metamodel/pkg/names" "github.com/openshift-online/ocm-api-metamodel/pkg/nomenclator" @@ -137,7 +138,7 @@ func (c *TypesCalculator) StructReference(typ *concepts.Type) *TypeReference { element := typ.Element() ref = &TypeReference{} ref.imprt, ref.selector = c.Package(element) - ref.name = goName(element) + ref.name = annotations.GoName(element) if ref.name == "" { ref.name = c.names.Public(element.Name()) } @@ -146,7 +147,7 @@ func (c *TypesCalculator) StructReference(typ *concepts.Type) *TypeReference { case typ.IsStruct(): ref = &TypeReference{} ref.imprt, ref.selector = c.Package(typ) - ref.name = goName(typ) + ref.name = annotations.GoName(typ) if ref.name == "" { ref.name = c.names.Public(typ.Name()) } @@ -199,7 +200,7 @@ func (c *TypesCalculator) ValueReference(typ *concepts.Type) *TypeReference { case typ.IsEnum(): ref = &TypeReference{} ref.imprt, ref.selector = c.Package(typ) - ref.name = goName(typ) + ref.name = annotations.GoName(typ) if ref.name == "" { ref.name = c.names.Public(typ.Name()) } @@ -227,7 +228,7 @@ func (c *TypesCalculator) ValueReference(typ *concepts.Type) *TypeReference { case typ.IsStruct(): ref = &TypeReference{} ref.imprt, ref.selector = c.Package(typ) - ref.name = goName(typ) + ref.name = annotations.GoName(typ) if ref.name == "" { ref.name = c.names.Public(typ.Name()) } @@ -276,7 +277,7 @@ func (c *TypesCalculator) ListReference(typ *concepts.Type) *TypeReference { element := typ.Element() ref := &TypeReference{} ref.imprt, ref.selector = c.Package(element) - ref.name = goName(element) + ref.name = annotations.GoName(element) if ref.name == "" { ref.name = c.names.Public(element.Name()) } @@ -359,7 +360,7 @@ func (c *TypesCalculator) BuilderReference(typ *concepts.Type) *TypeReference { case typ.IsStruct(): ref = &TypeReference{} ref.imprt, ref.selector = c.Package(typ) - ref.name = goName(typ) + ref.name = annotations.GoName(typ) if ref.name == "" { ref.name = c.names.Public(typ.Name()) } @@ -369,7 +370,7 @@ func (c *TypesCalculator) BuilderReference(typ *concepts.Type) *TypeReference { element := typ.Element() ref = &TypeReference{} ref.imprt, ref.selector = c.Package(element) - ref.name = goName(element) + ref.name = annotations.GoName(element) if ref.name == "" { ref.name = c.names.Public(element.Name()) } diff --git a/pkg/generators/golang/types_generator.go b/pkg/generators/golang/types_generator.go index 72b295d..f239fb1 100644 --- a/pkg/generators/golang/types_generator.go +++ b/pkg/generators/golang/types_generator.go @@ -19,6 +19,7 @@ package golang import ( "fmt" + "github.com/openshift-online/ocm-api-metamodel/pkg/annotations" "github.com/openshift-online/ocm-api-metamodel/pkg/concepts" "github.com/openshift-online/ocm-api-metamodel/pkg/http" "github.com/openshift-online/ocm-api-metamodel/pkg/names" @@ -607,7 +608,7 @@ func (g *TypesGenerator) getterType(attribute *concepts.Attribute) *TypeReferenc } func (g *TypesGenerator) objectName(typ *concepts.Type) string { - name := goName(typ) + name := annotations.GoName(typ) if name == "" { name = g.names.Public(typ.Name()) } @@ -615,11 +616,11 @@ func (g *TypesGenerator) objectName(typ *concepts.Type) string { } func (g *TypesGenerator) valueName(value *concepts.EnumValue) string { - typeName := goName(value.Type()) + typeName := annotations.GoName(value.Type()) if typeName == "" { typeName = g.names.Public(value.Type().Name()) } - valueName := goName(value) + valueName := annotations.GoName(value) if valueName == "" { valueName = g.names.Public(value.Name()) } @@ -631,7 +632,7 @@ func (g *TypesGenerator) valueTag(value *concepts.EnumValue) string { } func (g *TypesGenerator) getterName(attribute *concepts.Attribute) string { - name := goName(attribute) + name := annotations.GoName(attribute) if name == "" { name = g.names.Public(attribute.Name()) } @@ -666,7 +667,7 @@ func (g *TypesGenerator) fieldType(attribute *concepts.Attribute) *TypeReference } func (g *TypesGenerator) listName(typ *concepts.Type) string { - typeName := goName(typ) + typeName := annotations.GoName(typ) if typeName == "" { typeName = g.names.Public(typ.Name()) } diff --git a/pkg/http/binding_calculator.go b/pkg/http/binding_calculator.go index 89ace69..09040d7 100644 --- a/pkg/http/binding_calculator.go +++ b/pkg/http/binding_calculator.go @@ -25,6 +25,7 @@ import ( "net/http" "strconv" + "github.com/openshift-online/ocm-api-metamodel/pkg/annotations" "github.com/openshift-online/ocm-api-metamodel/pkg/concepts" "github.com/openshift-online/ocm-api-metamodel/pkg/nomenclator" "github.com/openshift-online/ocm-api-metamodel/pkg/reporter" @@ -182,7 +183,7 @@ func (c *BindingCalculator) DefaultStatus(method *concepts.Method) string { // AttributeName returns the field name corresponding to the given model attribute. func (c *BindingCalculator) AttributeName(attribute *concepts.Attribute) string { - name := jsonName(attribute) + name := annotations.JSONName(attribute) if name == "" { name = attribute.Name().Snake() } @@ -192,7 +193,7 @@ func (c *BindingCalculator) AttributeName(attribute *concepts.Attribute) string // ParameterName returns the name of the field or query parameter corresponding to the given model // method parameter. func (c *BindingCalculator) ParameterName(parameter *concepts.Parameter) string { - name := jsonName(parameter) + name := annotations.JSONName(parameter) if name == "" { name = parameter.Name().Snake() } @@ -201,7 +202,7 @@ func (c *BindingCalculator) ParameterName(parameter *concepts.Parameter) string // ServiceSegment calculates the URL segment corresponding to the given service. func (c *BindingCalculator) ServiceSegment(service *concepts.Service) string { - name := httpName(service) + name := annotations.HTTPName(service) if name == "" { name = service.Name().Snake() } @@ -210,7 +211,7 @@ func (c *BindingCalculator) ServiceSegment(service *concepts.Service) string { // VersionSegment calculates the URL segment corresponding to the given version. func (c *BindingCalculator) VersionSegment(version *concepts.Version) string { - name := httpName(version) + name := annotations.HTTPName(version) if name == "" { name = version.Name().Snake() } @@ -220,7 +221,7 @@ func (c *BindingCalculator) VersionSegment(version *concepts.Version) string { // LocatorSegment calculates the URL segment corresponding to the given method. func (c *BindingCalculator) MethodSegment(method *concepts.Method) string { if method.IsAction() { - name := httpName(method) + name := annotations.HTTPName(method) if name == "" { name = method.Name().Snake() } @@ -231,7 +232,7 @@ func (c *BindingCalculator) MethodSegment(method *concepts.Method) string { // LocatorSegment calculates the URL segment corresponding to the given locator. func (c *BindingCalculator) LocatorSegment(locator *concepts.Locator) string { - name := httpName(locator) + name := annotations.HTTPName(locator) if name == "" { name = locator.Name().Snake() } @@ -240,7 +241,7 @@ func (c *BindingCalculator) LocatorSegment(locator *concepts.Locator) string { // EnumValueName returns the name corresponding to a value of an enumerated type. func (c *BindingCalculator) EnumValueName(value *concepts.EnumValue) string { - name := jsonName(value) + name := annotations.JSONName(value) if name == "" { name = value.Name().Snake() } diff --git a/pkg/language/model_parser.go b/pkg/language/model_parser.go index a55553e..625ebe6 100644 --- a/pkg/language/model_parser.go +++ b/pkg/language/model_parser.go @@ -1273,7 +1273,9 @@ func (s *ClassDeclContext) Get_annotation() IAnnotationContext { return s._annot func (s *ClassDeclContext) GetName() IIdentifierContext { return s.name } -func (s *ClassDeclContext) Get_structMemberDecl() IStructMemberDeclContext { return s._structMemberDecl } +func (s *ClassDeclContext) Get_structMemberDecl() IStructMemberDeclContext { + return s._structMemberDecl +} func (s *ClassDeclContext) Set_annotation(v IAnnotationContext) { s._annotation = v } @@ -3673,7 +3675,9 @@ func (s *MethodParameterDeclContext) SetDflt(v ILiteralContext) { s.dflt = v } func (s *MethodParameterDeclContext) GetAnnotations() []IAnnotationContext { return s.annotations } -func (s *MethodParameterDeclContext) GetDirections() []IParameterDirectionContext { return s.directions } +func (s *MethodParameterDeclContext) GetDirections() []IParameterDirectionContext { + return s.directions +} func (s *MethodParameterDeclContext) SetAnnotations(v []IAnnotationContext) { s.annotations = v } @@ -5599,7 +5603,9 @@ func (s *AnnotationParametersContext) GetParameters() []IAnnotationParameterCont return s.parameters } -func (s *AnnotationParametersContext) SetParameters(v []IAnnotationParameterContext) { s.parameters = v } +func (s *AnnotationParametersContext) SetParameters(v []IAnnotationParameterContext) { + s.parameters = v +} func (s *AnnotationParametersContext) GetResult() map[string]interface{} { return s.result }