Skip to content

Commit

Permalink
fix: regex imports and validation when schema has pattern defined
Browse files Browse the repository at this point in the history
Signed-off-by: amy <[email protected]>
  • Loading branch information
amyXia1994 committed Nov 14, 2023
1 parent 35bf76e commit d9823b7
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
This file was generated by the KCL auto-gen tool. DO NOT EDIT.
Editing this file might prove futile when you re-run the KCL auto-gen generate command.
"""
import regex
import k8s.apimachinery.pkg.apis.meta.v1


Expand Down Expand Up @@ -148,7 +149,7 @@ schema CoreOamDevV1alpha2ContainerizedWorkloadSpecContainersItems0EnvItems0:


check:
matchPattern(name, ^[-_a-zA-Z0-9]+$)
regex.match(name, r"(^[-_a-zA-Z0-9]+$")


schema CoreOamDevV1alpha2ContainerizedWorkloadSpecContainersItems0LivenessProbe:
Expand Down Expand Up @@ -284,7 +285,7 @@ schema CoreOamDevV1alpha2ContainerizedWorkloadSpecContainersItems0PortsItems0:


check:
matchPattern(name, ^[a-z]+$)
regex.match(name, r"(^[a-z]+$")


schema CoreOamDevV1alpha2ContainerizedWorkloadSpecContainersItems0ReadiessProbe:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
This file was generated by the KCL auto-gen tool. DO NOT EDIT.
Editing this file might prove futile when you re-run the KCL auto-gen generate command.
"""
import regex
import k8s.apimachinery.pkg.apis.meta.v1


Expand Down Expand Up @@ -148,7 +149,7 @@ schema CoreOamDevV1alpha2ContainerizedWorkloadSpecContainersItems0EnvItems0:


check:
matchPattern(name, ^[-_a-zA-Z0-9]+$)
regex.match(name, r"(^[-_a-zA-Z0-9]+$")


schema CoreOamDevV1alpha2ContainerizedWorkloadSpecContainersItems0LivenessProbe:
Expand Down Expand Up @@ -284,7 +285,7 @@ schema CoreOamDevV1alpha2ContainerizedWorkloadSpecContainersItems0PortsItems0:


check:
matchPattern(name, ^[a-z]+$)
regex.match(name, r"(^[a-z]+$")


schema CoreOamDevV1alpha2ContainerizedWorkloadSpecContainersItems0ReadiessProbe:
Expand Down
52 changes: 44 additions & 8 deletions pkg/swagger/generator/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,31 +155,67 @@ func makeGenDefinitionHierarchy(name, pkg, container string, schema spec.Schema,
GenSchema: pg.GenSchema,
DependsOn: pg.Dependencies,
ExtraSchemas: gatherExtraSchemas(pg.ExtraSchemas),
Imports: collectSortedImports(pg.GenSchema),
Imports: pg.collectSortedImports(),
}, nil
}

type importStmt struct {
ImportPath string
AsName string
MustAsName bool
IsBuiltIn bool
}

func collectSortedImports(model GenSchema) []importStmt {
importMap := map[string]importStmt{}
collectImports(&model, model.Pkg, importMap)
sortedPkgPaths := make([]string, 0, len(importMap))
sortedImports := make([]importStmt, 0, len(importMap))
for k := range importMap {
func (sg *schemaGenContext) collectSortedImports() []importStmt {
// collect built-in imports
builtInImps := sg.GenSchema.getBuiltInImports()
for _, schema := range sg.ExtraSchemas {
imps := schema.getBuiltInImports()
for pkg, imp := range imps {
builtInImps[pkg] = imp
}
}

// collect pkg imports
pkgImps := map[string]importStmt{}
collectImports(&sg.GenSchema, sg.GenSchema.Pkg, pkgImps)

// sort imports with rules:
// 1. built-in imports always appears before pkg imports
// 2. the import paths are sorted in lexicographical order
sortedImports := sortImports(builtInImps)
for _, imp := range sortImports(pkgImps) {
sortedImports = append(sortedImports, imp)
}
return sortedImports
}

func sortImports(imports map[string]importStmt) []importStmt {
sortedPkgPaths := make([]string, 0, len(imports))
sortedImports := make([]importStmt, 0, len(imports))
for k := range imports {
sortedPkgPaths = append(sortedPkgPaths, k)
}
sort.Strings(sortedPkgPaths)
for _, k := range sortedPkgPaths {
sortedImports = append(sortedImports, importMap[k])
sortedImports = append(sortedImports, imports[k])
}
return sortedImports
}

func (schema *GenSchema) getBuiltInImports() map[string]importStmt {
imp := map[string]importStmt{}
for _, property := range schema.Properties {
if len(property.Pattern) != 0 {
imp["regex"] = importStmt{
ImportPath: "regex",
IsBuiltIn: true,
}
}
}
return imp
}

// getImportAsName infers the <import as> name by the context of all the existing import paths and the current pkg to be imported.
// the parent package name will be added as prefix to avoid import conflict
func getImportAsName(imp map[string]importStmt, pkg string, module string) string {
Expand Down
2 changes: 1 addition & 1 deletion pkg/swagger/generator/templates/schemavalidator.gotmpl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
len({{ .EscapedName }}) >= {{.MinLength}}
{{- end }}
{{- if .Pattern }}
matchPattern({{ .EscapedName }}, {{.Pattern}})
regex.match({{ .EscapedName }}, r"({{.Pattern}}"){{ if not .Required }} if {{ .EscapedName }}{{ end }}
{{- end }}
{{- if .UniqueItems }}
isunique({{ .EscapedName }})
Expand Down

0 comments on commit d9823b7

Please sign in to comment.