Skip to content

Commit

Permalink
Merge pull request #501 from muvaf/ignore-more
Browse files Browse the repository at this point in the history
Add FieldPaths to ignore rules
  • Loading branch information
jaypipes authored Nov 25, 2020
2 parents ff09e66 + fcb0133 commit 23e1073
Show file tree
Hide file tree
Showing 5 changed files with 319 additions and 323 deletions.
13 changes: 4 additions & 9 deletions pkg/generate/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ type IgnoreSpec struct {
// Set of shapes to ignore when constructing API type definitions and
// associated SDK code for structs that have these shapes as members
ShapeNames []string `json:"shape_names"`
// Set of field paths to ignore. The name here should be the original name of
// the field as it appears in AWS SDK objects. You can refer to a field by
// giving its "<shape_name>.<field_name>". For example, "CreateApiInput.Name".
FieldPaths []string `json:"field_paths"`
}

// ResourceConfig represents instructions to the ACK code generator
Expand Down Expand Up @@ -358,15 +362,6 @@ func (c *Config) SetAttributesSingleAttribute(resourceName string) bool {
return resGenConfig.UnpackAttributesMapConfig.SetAttributesSingleAttribute
}

// IsIgnoredShape returns true if the supplied shape name should be ignored by the
// code generator, false otherwise
func (c *Config) IsIgnoredShape(shapeName string) bool {
if c == nil || len(c.Ignore.ShapeNames) == 0 {
return false
}
return util.InStrings(shapeName, c.Ignore.ShapeNames)
}

// OverrideValues gives list of member values to override.
func (c *Config) OverrideValues(operationName string) (map[string]string, bool) {
if c == nil {
Expand Down
53 changes: 36 additions & 17 deletions pkg/generate/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,6 @@ func (g *Generator) GetCRDs() ([]*ackmodel.CRD, error) {
if memberShapeRef.Shape == nil {
return nil, ackmodel.ErrNilShapePointer
}
if g.cfg.IsIgnoredShape(memberShapeRef.Shape.ShapeName) {
continue
}
renamedName, _ := crd.InputFieldRename(
createOp.Name, memberName,
)
Expand Down Expand Up @@ -128,9 +125,6 @@ func (g *Generator) GetCRDs() ([]*ackmodel.CRD, error) {
}
}
for memberName, memberShapeRef := range outputShape.MemberRefs {
if g.cfg.IsIgnoredShape(memberShapeRef.Shape.ShapeName) {
continue
}
if memberShapeRef.Shape == nil {
return nil, ackmodel.ErrNilShapePointer
}
Expand Down Expand Up @@ -237,9 +231,6 @@ func (g *Generator) GetTypeDefs() ([]*ackmodel.TypeDef, map[string]string, error
// Neither are exceptions
continue
}
if g.cfg.IsIgnoredShape(shapeName) {
continue
}
tdefNames := names.New(shapeName)
if g.SDKAPI.HasConflictingTypeName(shapeName, g.cfg) {
tdefNames.Camel += ackmodel.ConflictingNameSuffix
Expand All @@ -250,9 +241,6 @@ func (g *Generator) GetTypeDefs() ([]*ackmodel.TypeDef, map[string]string, error
for memberName, memberRef := range shape.MemberRefs {
memberNames := names.New(memberName)
memberShape := memberRef.Shape
if g.cfg.IsIgnoredShape(memberShape.ShapeName) {
continue
}
if !g.IsShapeUsedInCRDs(memberShape.ShapeName) {
continue
}
Expand Down Expand Up @@ -374,6 +362,36 @@ func (g *Generator) GetEnumDefs() ([]*ackmodel.EnumDef, error) {
return edefs, nil
}

// ApplyShapeIgnoreRules removes the ignored shapes and fields from the API object
// so that they are not considered in any of the calculations of code generator.
func (g *Generator) ApplyShapeIgnoreRules() {
if g.cfg == nil || g.SDKAPI == nil {
return
}
for sdkShapeId, shape := range g.SDKAPI.API.Shapes {
for _, fieldpath := range g.cfg.Ignore.FieldPaths {
sn := strings.Split(fieldpath, ".")[0]
fn := strings.Split(fieldpath, ".")[1]
if shape.ShapeName != sn {
continue
}
delete(shape.MemberRefs, fn)
}
for _, sn := range g.cfg.Ignore.ShapeNames {
if shape.ShapeName == sn {
delete(g.SDKAPI.API.Shapes, sdkShapeId)
continue
}
// NOTE(muvaf): We need to remove the usage of the shape as well.
for sdkMemberId, memberRef := range shape.MemberRefs {
if memberRef.ShapeName == sn {
delete(shape.MemberRefs, sdkMemberId)
}
}
}
}
}

// New returns a new Generator struct for a supplied API model.
// Optionally, pass a file path to a generator config file that can be used to
// instruct the code generator how to handle the API properly
Expand All @@ -384,18 +402,19 @@ func New(
templateBasePath string,
defaultConfig ackgenconfig.Config,
) (*Generator, error) {
gc, err := ackgenconfig.New(configPath, defaultConfig)
cfg, err := ackgenconfig.New(configPath, defaultConfig)
if err != nil {
return nil, err
}

return &Generator{
g := &Generator{
SDKAPI: SDKAPI,
// TODO(jaypipes): Handle cases where service alias and service ID
// don't match (Step Functions)
serviceAlias: SDKAPI.ServiceID(),
apiVersion: apiVersion,
templateBasePath: templateBasePath,
cfg: &gc,
}, nil
cfg: &cfg,
}
g.ApplyShapeIgnoreRules()
return g, nil
}
Loading

0 comments on commit 23e1073

Please sign in to comment.