Skip to content

Add Regex Pattern to documentation #596

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions pkg/api/shape.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,17 @@ type Shape struct {
OriginalShapeName string `json:"-"`

// Map of exported member names to the ShapeReference.
MemberRefs map[string]*ShapeRef `json:"members"`
MemberRef ShapeRef `json:"member"` // List ref
KeyRef ShapeRef `json:"key"` // map key ref
ValueRef ShapeRef `json:"value"` // map value ref
Required []string
Payload string
Type string
MemberRefs map[string]*ShapeRef `json:"members"`
MemberRef ShapeRef `json:"member"` // List ref
KeyRef ShapeRef `json:"key"` // map key ref
ValueRef ShapeRef `json:"value"` // map value ref
Pattern string `json:"pattern"`
Required []string
Payload string
Type string
// this is being added for type union specifically. We want to generate
// api as struct and handle setSDK and setResource differently
RealType string
RealType string
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice gofmt

Exception bool
Enum []string
EnumConsts []string
Expand Down
6 changes: 6 additions & 0 deletions pkg/apiv2/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,12 @@ func createApiShape(shape Shape) (*awssdkmodel.Shape, error) {
if ok {
apiShape.DefaultValue = fmt.Sprintf("%v", val)
}

pattern, ok := shape.Traits["smithy.api#pattern"]
if ok {
apiShape.Pattern = pattern.(string)
}

documentation, _ := shape.Traits["smithy.api#documentation"].(string)
apiShape.Documentation = awssdkmodel.AppendDocstring("", documentation)

Expand Down
9 changes: 9 additions & 0 deletions pkg/model/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,15 @@ func (f *Field) GetDocumentation() string {
out.WriteString(f.ShapeRef.Documentation)
}
}

if f.ShapeRef != nil && f.ShapeRef.Shape != nil && f.ShapeRef.Shape.Pattern != "" {
if hasShapeDoc {
out.WriteString("\n//\n")
}
out.WriteString("// ")
out.WriteString(fmt.Sprintf("Regex Pattern: `%s`", f.ShapeRef.Shape.Pattern))
}

if cfg == nil {
return out.String()
}
Expand Down
31 changes: 31 additions & 0 deletions pkg/model/field_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,3 +338,34 @@ func TestFieldPathWithUnderscore(t *testing.T) {
field.Path = "subPathA.subPathB.MyField"
assert.Equal("subPathA_subPathB_MyField", field.FieldPathWithUnderscore())
}

func TestFieldWithPattern(t *testing.T) {
require := require.New(t)

g := testutil.NewModelForServiceWithOptions(t, "eks",
&testutil.TestingModelOptions{
GeneratorConfigFile: "generator.yaml",
DocumentationConfigFile: "documentation.yaml",
},
)

crds, err := g.GetCRDs()
require.Nil(err)

crd := getCRDByName("AddOn", crds)
require.NotNil(crd)

specFields := crd.SpecFields

// We have not altered the docstring for Version from the
// docstring that comes in the doc-2.json file...
ltdField := specFields["ClusterName"]
require.NotNil(ltdField)
require.NotNil(ltdField.ShapeRef)
require.NotEmpty(ltdField.ShapeRef.Shape.Pattern)

require.Equal(
"// The name of your cluster.\n//\n// Regex Pattern: `^[0-9A-Za-z][A-Za-z0-9\\-_]*$`",
ltdField.GetDocumentation(),
)
}