Skip to content

Commit

Permalink
Rename addTags to addComments in API transformer (istio-ecosystem…
Browse files Browse the repository at this point in the history
…#259)

Signed-off-by: Marko Lukša <mluksa@redhat.com>
Signed-off-by: Daniel Grimm <dgrimm@redhat.com>
  • Loading branch information
luksa authored and dgn committed Aug 13, 2024
1 parent 24adff8 commit 21035f3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
22 changes: 11 additions & 11 deletions hack/api_transformer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ type Transformations struct {
ReplaceFunctionReturnTypes map[string]string `yaml:"replaceFunctionReturnTypes"`
ReplaceFieldTypes map[string]string `yaml:"replaceFieldTypes"`
ReplaceTypes map[string]string `yaml:"replaceTypes"`
AddTags map[string]string `yaml:"addTags"`
AddComments map[string]string `yaml:"addComments"`
}

var config *Config
Expand Down Expand Up @@ -139,7 +139,7 @@ func merge(local, global *Transformations) *Transformations {
ReplaceFunctionReturnTypes: mergeStringMaps(local.ReplaceFunctionReturnTypes, global.ReplaceFunctionReturnTypes),
ReplaceFieldTypes: mergeStringMaps(local.ReplaceFieldTypes, global.ReplaceFieldTypes),
ReplaceTypes: mergeStringMaps(local.ReplaceTypes, global.ReplaceTypes),
AddTags: mergeStringMaps(local.AddTags, global.AddTags),
AddComments: mergeStringMaps(local.AddComments, global.AddComments),
}
}

Expand Down Expand Up @@ -240,17 +240,17 @@ func (t *FileTransformer) processFile() (*ast.File, error) {
field.Type = newType
}

if tag := t.getFieldTags(structName, fieldName); tag != "" {
addTag(field, tag)
if tag := t.getFieldComments(structName, fieldName); tag != "" {
addComment(field, tag)
}
if toString(field.Type) == "*intstr.IntOrString" {
addTag(field, "// +kubebuilder:validation:XIntOrString")
addComment(field, "// +kubebuilder:validation:XIntOrString")
}

if field.Doc != nil {
for _, comment := range field.Doc.List {
if strings.HasPrefix(comment.Text, "// REQUIRED.") {
addTag(field, "// +kubebuilder:validation:Required")
addComment(field, "// +kubebuilder:validation:Required")
removeOmitemptyFromJSONTag(field)
// TODO: remove pointer?
}
Expand Down Expand Up @@ -422,7 +422,7 @@ func convertTabsToHeadings(doc *ast.CommentGroup) {
}
}

func addTag(node ast.Node, text string) {
func addComment(node ast.Node, text string) {
switch n := node.(type) {
case *ast.Field:
if n.Doc == nil {
Expand Down Expand Up @@ -499,7 +499,7 @@ func processInterfaceFields(file *ast.File) {
newFields = append(newFields, field)
}
if hasInterfaceField {
addTag(genDecl, buildOneOfValidation(interfaceFields))
addComment(genDecl, buildOneOfValidation(interfaceFields))
}
structType.Fields.List = newFields
}
Expand Down Expand Up @@ -654,7 +654,7 @@ func convertEnum(enumName string, file *ast.File) {
if ident, ok := typeSpec.Type.(*ast.Ident); ok && ident.Name == "int32" {
ident.Name = "string"
}
addTag(genDecl, "// +kubebuilder:validation:Enum="+strings.Join(enumValues, ";"))
addComment(genDecl, "// +kubebuilder:validation:Enum="+strings.Join(enumValues, ";"))
}
case token.CONST:
// change the constant values to strings
Expand Down Expand Up @@ -745,8 +745,8 @@ func (t *FileTransformer) getFieldRename(structName string, fieldName string) st
return getMapValue(structName, fieldName, t.Transformations.RenameFields)
}

func (t *FileTransformer) getFieldTags(structName string, fieldName string) string {
return getMapValue(structName, fieldName, t.Transformations.AddTags)
func (t *FileTransformer) getFieldComments(structName string, fieldName string) string {
return getMapValue(structName, fieldName, t.Transformations.AddComments)
}

func getMapValue(parent string, child string, m map[string]string) string {
Expand Down
4 changes: 2 additions & 2 deletions hack/api_transformer/transform.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ inputFiles:
SidecarInjectorConfig.AlwaysInjectSelector: "[]metav1.LabelSelector"
SidecarInjectorConfig.InjectedAnnotations: "map[string]string"
SidecarInjectorConfig.Templates: "map[string]string"
addTags:
addComments:
CNIConfig.PullPolicy: "// +kubebuilder:validation:Enum=Always;Never;IfNotPresent"
GlobalConfig.ImagePullPolicy: "// +kubebuilder:validation:Enum=Always;Never;IfNotPresent"

Expand All @@ -129,7 +129,7 @@ inputFiles:
removeTypes:
- LabelSelector
- LabelSelectorRequirement
addTags:
addComments:
MeshConfig.ExtensionProviders: "// +kubebuilder:validation:MaxItems=1000" # to prevent CEL cost overruns

- module: istio.io/api
Expand Down

0 comments on commit 21035f3

Please sign in to comment.