Skip to content

Commit e0febaf

Browse files
authored
feat: mangle property name with quote chars (#110)
Signed-off-by: peefy <[email protected]>
1 parent 1b4225b commit e0febaf

File tree

6 files changed

+64
-13
lines changed

6 files changed

+64
-13
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""
2+
This file was generated by the KCL auto-gen tool. DO NOT EDIT.
3+
Editing this file might prove futile when you re-run the KCL auto-gen generate command.
4+
"""
5+
6+
7+
schema My_Config:
8+
r"""
9+
my config
10+
11+
Attributes
12+
----------
13+
"my-name" : str, default is Undefined, optional
14+
my name
15+
"my-labels" : {str:int}, default is Undefined, optional
16+
my labels
17+
"""
18+
19+
20+
"my-name"?: str
21+
22+
"my-labels"?: {str:int}
23+
24+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
definitions:
2+
My-Config:
3+
type: object
4+
properties:
5+
my-name:
6+
type: string
7+
my-labels:
8+
type: object
9+
additionalProperties:
10+
type: integer
11+
swagger: "2.0"
12+
info:
13+
title: kcl
14+
version: v0.0.2
15+
paths: {}

pkg/kube_resource/generator/testdata/array_branch/models/operator_victoriametrics_com_v1beta1_vm_agent.k

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ schema OperatorVictoriametricsComV1beta1VMAgentSpec:
4343

4444
Attributes
4545
----------
46-
_ : str, default is Undefined, optional
46+
"-" : str, default is Undefined, optional
4747
ParsingError contents error with context if operator was failed to parse json object from kubernetes api server
4848
affinity : any, default is Undefined, optional
4949
Affinity If specified, the pod's scheduling constraints.
@@ -208,7 +208,7 @@ schema OperatorVictoriametricsComV1beta1VMAgentSpec:
208208
"""
209209

210210

211-
_?: str
211+
"-"?: str
212212

213213
affinity?: any
214214

pkg/swagger/generator/language.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"path"
2222
"path/filepath"
2323
"reflect"
24+
"regexp"
2425
"sort"
2526
"strings"
2627

@@ -32,6 +33,7 @@ import (
3233
var (
3334
// DefaultLanguageFunc defines the default generation language
3435
DefaultLanguageFunc func() *LanguageOpts
36+
validNameRegexp = regexp.MustCompile(`\$?^[a-zA-Z_][a-zA-Z0-9_]*$`)
3537
)
3638

3739
const (
@@ -91,12 +93,12 @@ func (l *LanguageOpts) MangleVarName(name string) string {
9193

9294
// MangleModelName adds "$" prefix to name if it is conflict with KCL keyword
9395
func (l *LanguageOpts) MangleModelName(modelName string) string {
94-
// replace all the "-" to "_" in the model name
9596
lastDotIndex := strings.LastIndex(modelName, ".")
9697
shortName := modelName[lastDotIndex+1:]
97-
if strings.Contains(shortName, "-") {
98-
log.Printf("[WARN] the modelName %s contains symbol '-' which is forbidden in KCL. Will be replaced by '_'", shortName)
99-
modelName = modelName[:lastDotIndex+1] + strings.Replace(shortName, "-", "_", -1)
98+
// Replace all the "-" to "_" in the model name
99+
if strings.Contains(shortName, "-") || strings.Contains(shortName, ".") {
100+
log.Printf("[WARN] the modelName %s contains symbols '-' or '.' which is forbidden in KCL. Will be replaced by '_'", shortName)
101+
modelName = modelName[:lastDotIndex+1] + strings.Replace(strings.Replace(shortName, "-", "_", -1), ".", "_", -1)
100102
}
101103
for _, kw := range l.ReservedWords {
102104
if modelName == kw {
@@ -106,6 +108,19 @@ func (l *LanguageOpts) MangleModelName(modelName string) string {
106108
return modelName
107109
}
108110

111+
// ManglePropertyName adds "$" prefix to name if it is conflict with KCL keyword or adds quotes "
112+
func (l *LanguageOpts) ManglePropertyName(name string) string {
113+
if !validNameRegexp.MatchString(name) {
114+
name = fmt.Sprintf(`"%s"`, name)
115+
}
116+
for _, kw := range l.ReservedWords {
117+
if name == kw {
118+
return fmt.Sprintf("$%s", name)
119+
}
120+
}
121+
return name
122+
}
123+
109124
// MangleFileName makes sure a file name gets a safe name
110125
func (l *LanguageOpts) MangleFileName(name string) string {
111126
if l.fileNameFunc != nil {

pkg/swagger/generator/model.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1184,7 +1184,7 @@ func (sg *schemaGenContext) buildItems() error {
11841184
// This is a tuple, build a new model that represents this
11851185
if sg.Named {
11861186
sg.GenSchema.Name = sg.Name
1187-
sg.GenSchema.EscapedName = DefaultLanguageFunc().MangleModelName(sg.GenSchema.Name)
1187+
sg.GenSchema.EscapedName = DefaultLanguageFunc().ManglePropertyName(sg.GenSchema.Name)
11881188
sg.GenSchema.KclType = sg.TypeResolver.kclTypeName(sg.Name)
11891189
for i, s := range sg.Schema.Items.Schemas {
11901190
elProp := sg.NewTupleElement(&s, i)
@@ -1211,7 +1211,7 @@ func (sg *schemaGenContext) buildItems() error {
12111211
}
12121212
sg.MergeResult(elProp, false)
12131213
elProp.GenSchema.Name = "p" + strconv.Itoa(i)
1214-
elProp.GenSchema.EscapedName = DefaultLanguageFunc().MangleModelName(elProp.GenSchema.Name)
1214+
elProp.GenSchema.EscapedName = DefaultLanguageFunc().ManglePropertyName(elProp.GenSchema.Name)
12151215
sg.GenSchema.Properties = append(sg.GenSchema.Properties, elProp.GenSchema)
12161216
sg.GenSchema.IsTuple = true
12171217
}
@@ -1433,7 +1433,7 @@ func (sg *schemaGenContext) makeGenSchema() error {
14331433
sg.GenSchema.KeyVar = sg.KeyVar
14341434
sg.GenSchema.OriginalName = sg.Name
14351435
sg.GenSchema.Name = sg.KclName()
1436-
sg.GenSchema.EscapedName = DefaultLanguageFunc().MangleModelName(sg.GenSchema.Name)
1436+
sg.GenSchema.EscapedName = DefaultLanguageFunc().ManglePropertyName(sg.GenSchema.Name)
14371437
sg.GenSchema.Title = sg.Schema.Title
14381438
sg.GenSchema.Description = trimBOM(sg.Schema.Description)
14391439
sg.GenSchema.ReceiverName = sg.Receiver

pkg/swagger/generator/template_repo.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,15 +133,12 @@ func DefaultFuncMap(lang *LanguageOpts) template.FuncMap {
133133
var properties GenSchemaList
134134
for _, one := range allOf {
135135
if !one.IsBaseType {
136-
for _, p := range one.Properties {
137-
properties = append(properties, p)
138-
}
136+
properties = append(properties, one.Properties...)
139137
}
140138
}
141139
return properties
142140
},
143141
"toKCLValue": lang.ToKclValue,
144-
"escapeKeyword": lang.MangleModelName,
145142
"nonEmptyValue": lang.NonEmptyValue,
146143
}
147144
}

0 commit comments

Comments
 (0)