Skip to content

Commit cfe9477

Browse files
authored
Add Regex Pattern to documentation (#596)
Issue #, if available: [930](aws-controllers-k8s/community#930) Description of changes: - Read smithy.api#pattern from aws-sdk-go-v2 JSON models - Add Regex pattern to documentation string if exists - Add test to verify pattern string is added when present By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent ed3e8af commit cfe9477

File tree

4 files changed

+55
-8
lines changed

4 files changed

+55
-8
lines changed

pkg/api/shape.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,16 +90,17 @@ type Shape struct {
9090
OriginalShapeName string `json:"-"`
9191

9292
// Map of exported member names to the ShapeReference.
93-
MemberRefs map[string]*ShapeRef `json:"members"`
94-
MemberRef ShapeRef `json:"member"` // List ref
95-
KeyRef ShapeRef `json:"key"` // map key ref
96-
ValueRef ShapeRef `json:"value"` // map value ref
97-
Required []string
98-
Payload string
99-
Type string
93+
MemberRefs map[string]*ShapeRef `json:"members"`
94+
MemberRef ShapeRef `json:"member"` // List ref
95+
KeyRef ShapeRef `json:"key"` // map key ref
96+
ValueRef ShapeRef `json:"value"` // map value ref
97+
Pattern string `json:"pattern"`
98+
Required []string
99+
Payload string
100+
Type string
100101
// this is being added for type union specifically. We want to generate
101102
// api as struct and handle setSDK and setResource differently
102-
RealType string
103+
RealType string
103104
Exception bool
104105
Enum []string
105106
EnumConsts []string

pkg/apiv2/converter.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,12 @@ func createApiShape(shape Shape) (*awssdkmodel.Shape, error) {
231231
if ok {
232232
apiShape.DefaultValue = fmt.Sprintf("%v", val)
233233
}
234+
235+
pattern, ok := shape.Traits["smithy.api#pattern"]
236+
if ok {
237+
apiShape.Pattern = pattern.(string)
238+
}
239+
234240
documentation, _ := shape.Traits["smithy.api#documentation"].(string)
235241
apiShape.Documentation = awssdkmodel.AppendDocstring("", documentation)
236242

pkg/model/field.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,15 @@ func (f *Field) GetDocumentation() string {
104104
out.WriteString(f.ShapeRef.Documentation)
105105
}
106106
}
107+
108+
if f.ShapeRef != nil && f.ShapeRef.Shape != nil && f.ShapeRef.Shape.Pattern != "" {
109+
if hasShapeDoc {
110+
out.WriteString("\n//\n")
111+
}
112+
out.WriteString("// ")
113+
out.WriteString(fmt.Sprintf("Regex Pattern: `%s`", f.ShapeRef.Shape.Pattern))
114+
}
115+
107116
if cfg == nil {
108117
return out.String()
109118
}

pkg/model/field_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,3 +338,34 @@ func TestFieldPathWithUnderscore(t *testing.T) {
338338
field.Path = "subPathA.subPathB.MyField"
339339
assert.Equal("subPathA_subPathB_MyField", field.FieldPathWithUnderscore())
340340
}
341+
342+
func TestFieldWithPattern(t *testing.T) {
343+
require := require.New(t)
344+
345+
g := testutil.NewModelForServiceWithOptions(t, "eks",
346+
&testutil.TestingModelOptions{
347+
GeneratorConfigFile: "generator.yaml",
348+
DocumentationConfigFile: "documentation.yaml",
349+
},
350+
)
351+
352+
crds, err := g.GetCRDs()
353+
require.Nil(err)
354+
355+
crd := getCRDByName("AddOn", crds)
356+
require.NotNil(crd)
357+
358+
specFields := crd.SpecFields
359+
360+
// We have not altered the docstring for Version from the
361+
// docstring that comes in the doc-2.json file...
362+
ltdField := specFields["ClusterName"]
363+
require.NotNil(ltdField)
364+
require.NotNil(ltdField.ShapeRef)
365+
require.NotEmpty(ltdField.ShapeRef.Shape.Pattern)
366+
367+
require.Equal(
368+
"// The name of your cluster.\n//\n// Regex Pattern: `^[0-9A-Za-z][A-Za-z0-9\\-_]*$`",
369+
ltdField.GetDocumentation(),
370+
)
371+
}

0 commit comments

Comments
 (0)