Add toEscaped API

This commit is contained in:
Masaaki Goshima 2020-12-25 16:38:18 +09:00
parent 9fb34e4449
commit 5de8891c0c
2 changed files with 11671 additions and 7 deletions

View File

@ -15,6 +15,7 @@ type opType struct {
Op string
Code string
Indent func() string
Escaped func() string
HeadToPtrHead func() string
HeadToNPtrHead func() string
HeadToAnonymousHead func() string
@ -30,6 +31,7 @@ func createOpType(op, code string) opType {
Op: op,
Code: code,
Indent: func() string { return fmt.Sprintf("%sIndent", op) },
Escaped: func() string { return op },
HeadToPtrHead: func() string { return op },
HeadToNPtrHead: func() string { return op },
HeadToAnonymousHead: func() string { return op },
@ -91,6 +93,16 @@ func (t opType) toIndent() opType {
return t
}
func (t opType) toEscaped() opType {
switch t {
{{- range $type := .OpTypes }}
case op{{ $type.Op }}:
return op{{ call $type.Escaped }}
{{- end }}
}
return t
}
func (t opType) headToPtrHead() opType {
switch t {
{{- range $type := .OpTypes }}
@ -231,7 +243,14 @@ func (t opType) fieldToStringTagField() opType {
}
for _, typ := range primitiveTypesUpper {
typ := typ
opTypes = append(opTypes, createOpType(typ, "Op"))
optype := createOpType(typ, "Op")
switch typ {
case "String", "StringPtr", "StringNPtr":
optype.Escaped = func() string {
return fmt.Sprintf("Escaped%s", typ)
}
}
opTypes = append(opTypes, optype)
}
for _, escapedOrNot := range []string{"", "Escaped"} {
for _, ptrOrNot := range []string{"", "Ptr", "NPtr"} {
@ -256,6 +275,25 @@ func (t opType) fieldToStringTagField() opType {
Op: op,
Code: "StructField",
Indent: func() string { return fmt.Sprintf("%sIndent", op) },
Escaped: func() string {
switch typ {
case "String", "StringPtr", "StringNPtr":
return fmt.Sprintf(
"StructEscapedField%s%sHead%sEscaped%s",
ptrOrNot,
headType,
opt,
typ,
)
}
return fmt.Sprintf(
"StructEscapedField%s%sHead%s%s",
ptrOrNot,
headType,
opt,
typ,
)
},
HeadToPtrHead: func() string {
return fmt.Sprintf(
"Struct%sFieldPtr%sHead%s%s",
@ -335,6 +373,21 @@ func (t opType) fieldToStringTagField() opType {
Op: op,
Code: "StructField",
Indent: func() string { return fmt.Sprintf("%sIndent", op) },
Escaped: func() string {
switch typ {
case "String", "StringPtr", "StringNPtr":
return fmt.Sprintf(
"StructEscapedField%sEscaped%s",
opt,
typ,
)
}
return fmt.Sprintf(
"StructEscapedField%s%s",
opt,
typ,
)
},
HeadToPtrHead: func() string { return op },
HeadToNPtrHead: func() string { return op },
HeadToAnonymousHead: func() string { return op },
@ -366,6 +419,7 @@ func (t opType) fieldToStringTagField() opType {
Op: fmt.Sprintf("%sIndent", typ.Op),
Code: typ.Code,
Indent: func() string { return fmt.Sprintf("%sIndent", typ.Op) },
Escaped: func() string { return fmt.Sprintf("%sIndent", typ.Escaped()) },
HeadToPtrHead: func() string {
return fmt.Sprintf("%sIndent", typ.HeadToPtrHead())
},

File diff suppressed because it is too large Load Diff