Skip to content

Commit

Permalink
Use a string store to tell what strings to use to the code generator …
Browse files Browse the repository at this point in the history
…so that customization for custom generations are easier to achieve

Signed-off-by: Muvaffak Onus <onus.muvaffak@gmail.com>
  • Loading branch information
muvaf committed Oct 26, 2020
1 parent c09fcea commit c32a8d5
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 13 deletions.
2 changes: 1 addition & 1 deletion cmd/ack-generate/command/apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion cmd/ack-generate/command/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions pkg/generate/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -379,5 +381,6 @@ func New(
apiVersion: apiVersion,
templateBasePath: templateBasePath,
cfg: gc,
stringStore: stringStore,
}, nil
}
20 changes: 12 additions & 8 deletions pkg/model/crd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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*
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
}
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand All @@ -2431,6 +2434,7 @@ func NewCRD(
Ops: crdOps,
SpecFields: map[string]*CRDField{},
StatusFields: map[string]*CRDField{},
StringStore: stringStore,
}
}

Expand Down
38 changes: 38 additions & 0 deletions pkg/model/store.go
Original file line number Diff line number Diff line change
@@ -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{}",
}
}
2 changes: 1 addition & 1 deletion pkg/testutil/schema_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit c32a8d5

Please sign in to comment.