Skip to content

Commit

Permalink
Make annotation helpers public
Browse files Browse the repository at this point in the history
Currently the functions used to get the `name` parameter of the `@json`,
`@http` and `@go` annotations are private in the packages that use it.
This makes it difficult to use them in other packages, in particular
they can't be used in the OpenAPI generator package, and that is
necessary in order to fix a bug. This patch moves those functions to a
new `annotations` package and makes them public. The OpenAPI generator
bug will be fixed in a separate commit.

Signed-off-by: Juan Hernandez <[email protected]>
  • Loading branch information
jhernand committed May 18, 2023
1 parent 01e4c03 commit 6563424
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 94 deletions.
24 changes: 19 additions & 5 deletions pkg/http/annotations.go → pkg/annotations/annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package http
package annotations

import (
"fmt"

"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 ""
Expand All @@ -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 ""
Expand All @@ -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)
}
37 changes: 0 additions & 37 deletions pkg/generators/golang/annotations.go

This file was deleted.

15 changes: 8 additions & 7 deletions pkg/generators/golang/builders_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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())
}
Expand All @@ -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())
}
Expand All @@ -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())
}
Expand Down Expand Up @@ -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())
}
Expand Down
31 changes: 16 additions & 15 deletions pkg/generators/golang/clients_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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())
}
Expand All @@ -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())
}
Expand Down Expand Up @@ -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())
}
Expand All @@ -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())
}
Expand Down Expand Up @@ -1102,15 +1103,15 @@ 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())
}
return name
}

func (g *ClientsGenerator) methodName(method *concepts.Method) string {
name := goName(method)
name := annotations.GoName(method)
if name == "" {
name = g.names.Public(method.Name())
}
Expand All @@ -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())
}
Expand All @@ -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())
}
Expand All @@ -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())
}
Expand All @@ -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())
}
Expand All @@ -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())
}
Expand Down
17 changes: 9 additions & 8 deletions pkg/generators/golang/json_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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())
}
Expand All @@ -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())
}
Expand All @@ -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())
}
Expand All @@ -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())
}
Expand Down
Loading

0 comments on commit 6563424

Please sign in to comment.