From c32a8d55f46b613a410a80412f7e4b807b0d12fc Mon Sep 17 00:00:00 2001 From: Muvaffak Onus Date: Mon, 26 Oct 2020 15:48:46 +0300 Subject: [PATCH] Use a string store to tell what strings to use to the code generator so that customization for custom generations are easier to achieve Signed-off-by: Muvaffak Onus --- cmd/ack-generate/command/apis.go | 2 +- cmd/ack-generate/command/controller.go | 2 +- pkg/generate/generator.go | 7 +++-- pkg/model/crd.go | 20 ++++++++------ pkg/model/store.go | 38 ++++++++++++++++++++++++++ pkg/testutil/schema_helper.go | 2 +- 6 files changed, 58 insertions(+), 13 deletions(-) create mode 100644 pkg/model/store.go diff --git a/cmd/ack-generate/command/apis.go b/cmd/ack-generate/command/apis.go index 64ec396531..3fc98bf8d1 100644 --- a/cmd/ack-generate/command/apis.go +++ b/cmd/ack-generate/command/apis.go @@ -90,7 +90,7 @@ func generateAPIs(cmd *cobra.Command, args []string) error { } } g, err := generate.New( - sdkAPI, optGenVersion, optGeneratorConfigPath, optTemplatesDir, + sdkAPI, optGenVersion, optGeneratorConfigPath, optTemplatesDir, model.DefaultStringStore(), ) if err != nil { return err diff --git a/cmd/ack-generate/command/controller.go b/cmd/ack-generate/command/controller.go index df2efa20ae..110d0550f7 100644 --- a/cmd/ack-generate/command/controller.go +++ b/cmd/ack-generate/command/controller.go @@ -80,7 +80,7 @@ func generateController(cmd *cobra.Command, args []string) error { return err } g, err := generate.New( - sdkAPI, latestAPIVersion, optGeneratorConfigPath, optTemplatesDir, + sdkAPI, latestAPIVersion, optGeneratorConfigPath, optTemplatesDir, ackmodel.DefaultStringStore(), ) if err != nil { return err diff --git a/pkg/generate/generator.go b/pkg/generate/generator.go index da1bb7949d..43d252ba25 100644 --- a/pkg/generate/generator.go +++ b/pkg/generate/generator.go @@ -38,7 +38,8 @@ type Generator struct { typeRenames map[string]string // Instructions to the code generator how to handle the API and its // resources - cfg *ackgenconfig.Config + cfg *ackgenconfig.Config + stringStore map[ackmodel.StringIdentifier]string } // GetCRDs returns a slice of `ackmodel.CRD` structs that describe the @@ -74,7 +75,7 @@ func (g *Generator) GetCRDs() ([]*ackmodel.CRD, error) { SetAttributes: setAttributesOps[crdName], } g.RemoveIgnoredOperations(&crdOps) - crd := ackmodel.NewCRD(g.SDKAPI, g.cfg, crdNames, crdOps) + crd := ackmodel.NewCRD(g.SDKAPI, g.cfg, crdNames, crdOps, g.stringStore) // OK, begin to gather the CRDFields that will go into the Spec struct. // These fields are those members of the Create operation's Input @@ -361,6 +362,7 @@ func New( apiVersion string, configPath string, templateBasePath string, + stringStore map[ackmodel.StringIdentifier]string, ) (*Generator, error) { var gc *ackgenconfig.Config var err error @@ -379,5 +381,6 @@ func New( apiVersion: apiVersion, templateBasePath: templateBasePath, cfg: gc, + stringStore: stringStore, }, nil } diff --git a/pkg/model/crd.go b/pkg/model/crd.go index 3983d39cc0..2f013176ec 100644 --- a/pkg/model/crd.go +++ b/pkg/model/crd.go @@ -124,6 +124,8 @@ type CRD struct { // TypeImports is a map, keyed by an import string, with the map value // being the import alias TypeImports map[string]string + // StringStore includes the constant strings that are used in the code generation. + StringStore map[StringIdentifier]string } // HasShapeAsMember returns true if the supplied Shape name appears in *any* @@ -732,14 +734,14 @@ func (r *CRD) GoCodeSetInput( sourceAdaptedVarName := sourceVarName crdField, found = r.SpecFields[renamedName] if found { - sourceAdaptedVarName += ".Spec" + sourceAdaptedVarName += r.StringStore[SpecFieldPath] } else { crdField, found = r.StatusFields[memberName] if !found { // TODO(jaypipes): check generator config for exceptions? continue } - sourceAdaptedVarName += ".Status" + sourceAdaptedVarName += r.StringStore[StatusFieldPath] } sourceAdaptedVarName += "." + crdField.Names.Camel @@ -1671,7 +1673,7 @@ func (r *CRD) GoCodeSetOutput( targetAdaptedVarName := targetVarName crdField, found = r.SpecFields[memberName] if found { - targetAdaptedVarName += ".Spec" + targetAdaptedVarName += r.StringStore[SpecFieldPath] if !performSpecUpdate { continue } @@ -1681,7 +1683,7 @@ func (r *CRD) GoCodeSetOutput( // TODO(jaypipes): check generator config for exceptions? continue } - targetAdaptedVarName += ".Status" + targetAdaptedVarName += r.StringStore[StatusFieldPath] } targetMemberShapeRef = crdField.ShapeRef // fieldVarName is the name of the variable that is used for temporary @@ -1858,7 +1860,7 @@ func (r *CRD) goCodeSetOutputReadMany( "%sif len(%s.%s) == 0 {\n", indent, sourceVarName, listShapeName, ) - out += fmt.Sprintf("%s\treturn nil, ackerr.NotFound\n", indent) + out += fmt.Sprintf("%s\treturn %s, ackerr.NotFound\n", indent, r.StringStore[FindNilReturn]) out += fmt.Sprintf("%s}\n", indent) // found := false @@ -1918,14 +1920,14 @@ func (r *CRD) goCodeSetOutputReadMany( targetAdaptedVarName := targetVarName crdField, found = r.SpecFields[memberName] if found { - targetAdaptedVarName += ".Spec" + targetAdaptedVarName += r.StringStore[SpecFieldPath] } else { crdField, found = r.StatusFields[memberName] if !found { // TODO(jaypipes): check generator config for exceptions? continue } - targetAdaptedVarName += ".Status" + targetAdaptedVarName += r.StringStore[StatusFieldPath] } targetMemberShapeRef = crdField.ShapeRef out += fmt.Sprintf( @@ -2016,7 +2018,7 @@ func (r *CRD) goCodeSetOutputReadMany( // return nil, ackerr.NotFound // } out += fmt.Sprintf("%sif !found {\n", indent) - out += fmt.Sprintf("%s\treturn nil, ackerr.NotFound\n", indent) + out += fmt.Sprintf("%s\treturn %s, ackerr.NotFound\n", indent, r.StringStore[FindNilReturn]) out += fmt.Sprintf("%s}\n", indent) return out } @@ -2418,6 +2420,7 @@ func NewCRD( genCfg *ackgenconfig.Config, crdNames names.Names, crdOps CRDOps, + stringStore map[StringIdentifier]string, ) *CRD { pluralize := pluralize.NewClient() kind := crdNames.Camel @@ -2431,6 +2434,7 @@ func NewCRD( Ops: crdOps, SpecFields: map[string]*CRDField{}, StatusFields: map[string]*CRDField{}, + StringStore: stringStore, } } diff --git a/pkg/model/store.go b/pkg/model/store.go new file mode 100644 index 0000000000..e1b0645e29 --- /dev/null +++ b/pkg/model/store.go @@ -0,0 +1,38 @@ +// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"). You may +// not use this file except in compliance with the License. A copy of the +// License is located at +// +// http://aws.amazon.com/apache2.0/ +// +// or in the "license" file accompanying this file. This file 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 model + +type StringIdentifier int + +const ( + SpecFieldPath StringIdentifier = iota + StatusFieldPath + FindNilReturn +) + +func DefaultStringStore() map[StringIdentifier]string { + return map[StringIdentifier]string{ + SpecFieldPath: ".Spec", + StatusFieldPath: ".Status", + FindNilReturn: "nil", + } +} + +func CrossplaneStringStore() map[StringIdentifier]string { + return map[StringIdentifier]string{ + SpecFieldPath: ".Spec.ForProvider", + StatusFieldPath: ".Status.AtProvider", + FindNilReturn: "managed.ExternalObservation{}", + } +} diff --git a/pkg/testutil/schema_helper.go b/pkg/testutil/schema_helper.go index bb30c99290..1c0c893dbf 100644 --- a/pkg/testutil/schema_helper.go +++ b/pkg/testutil/schema_helper.go @@ -33,7 +33,7 @@ func NewGeneratorForService(t *testing.T, serviceAlias string) *generate.Generat if _, err := os.Stat(generatorConfigPath); os.IsNotExist(err) { generatorConfigPath = "" } - g, err := generate.New(sdkAPI, "v1alpha1", generatorConfigPath, "") + g, err := generate.New(sdkAPI, "v1alpha1", generatorConfigPath, "", model.DefaultStringStore()) if err != nil { t.Fatal(err) }