Skip to content
Prev Previous commit
Next Next commit
remove hyphens and underscores in references
  • Loading branch information
rquitales committed Sep 13, 2024
commit 01c3a410b4fe908d4788c70f61d1662a3b5aca7c
9 changes: 7 additions & 2 deletions pkg/codegen/customresourcegenerator.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func flattenRecursively(sw *spec.Swagger, parentName string, currSpec spec.Schem
for nestedPropertyName, nestedProperty := range currSpec.Properties {
// Create a new definition for the nested object by joining the parent definition name and the property name.
// This is to ensure that the nested object is unique and does not conflict with other definitions.
nestedDefinitionName := parentName + sanitizeFieldName(nestedPropertyName)
nestedDefinitionName := parentName + sanitizeReferenceName(nestedPropertyName)

s, err := flattenRecursively(sw, nestedDefinitionName, nestedProperty)
if err != nil {
Expand Down Expand Up @@ -164,11 +164,16 @@ func flattenRecursively(sw *spec.Swagger, parentName string, currSpec spec.Schem
return currSpec, nil
}

func sanitizeFieldName(fieldName string) string {
func sanitizeReferenceName(fieldName string) string {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this basically responsible for ensuring the ref name is a valid token in all of our target languages?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes! Previously, in crd2pulumi, we need to do this in a number of locations which resulted in harder to maintain code, and the bugs we saw. For package name sanitization, that resides in the p-k schemagen logic.

// If the field name is "arg" or "args", we need to change it to "Arguments" to avoid conflicts with Go reserved words.
if s := strings.ToLower(fieldName); s == "arg" || s == "args" {
return "Arguments"
}

//We need to strip out any hyphens and underscores in the reference.
fieldName = cgstrings.Unhyphenate(fieldName)
fieldName = cgstrings.ModifyStringAroundDelimeter(fieldName, "_", cgstrings.UppercaseFirst)

return cgstrings.UppercaseFirst(fieldName)
}

Expand Down