forked from mirror/go-json
Add toEscaped API
This commit is contained in:
parent
9fb34e4449
commit
5de8891c0c
|
@ -15,6 +15,7 @@ type opType struct {
|
||||||
Op string
|
Op string
|
||||||
Code string
|
Code string
|
||||||
Indent func() string
|
Indent func() string
|
||||||
|
Escaped func() string
|
||||||
HeadToPtrHead func() string
|
HeadToPtrHead func() string
|
||||||
HeadToNPtrHead func() string
|
HeadToNPtrHead func() string
|
||||||
HeadToAnonymousHead func() string
|
HeadToAnonymousHead func() string
|
||||||
|
@ -30,6 +31,7 @@ func createOpType(op, code string) opType {
|
||||||
Op: op,
|
Op: op,
|
||||||
Code: code,
|
Code: code,
|
||||||
Indent: func() string { return fmt.Sprintf("%sIndent", op) },
|
Indent: func() string { return fmt.Sprintf("%sIndent", op) },
|
||||||
|
Escaped: func() string { return op },
|
||||||
HeadToPtrHead: func() string { return op },
|
HeadToPtrHead: func() string { return op },
|
||||||
HeadToNPtrHead: func() string { return op },
|
HeadToNPtrHead: func() string { return op },
|
||||||
HeadToAnonymousHead: func() string { return op },
|
HeadToAnonymousHead: func() string { return op },
|
||||||
|
@ -91,6 +93,16 @@ func (t opType) toIndent() opType {
|
||||||
return t
|
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 {
|
func (t opType) headToPtrHead() opType {
|
||||||
switch t {
|
switch t {
|
||||||
{{- range $type := .OpTypes }}
|
{{- range $type := .OpTypes }}
|
||||||
|
@ -231,7 +243,14 @@ func (t opType) fieldToStringTagField() opType {
|
||||||
}
|
}
|
||||||
for _, typ := range primitiveTypesUpper {
|
for _, typ := range primitiveTypesUpper {
|
||||||
typ := typ
|
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 _, escapedOrNot := range []string{"", "Escaped"} {
|
||||||
for _, ptrOrNot := range []string{"", "Ptr", "NPtr"} {
|
for _, ptrOrNot := range []string{"", "Ptr", "NPtr"} {
|
||||||
|
@ -256,6 +275,25 @@ func (t opType) fieldToStringTagField() opType {
|
||||||
Op: op,
|
Op: op,
|
||||||
Code: "StructField",
|
Code: "StructField",
|
||||||
Indent: func() string { return fmt.Sprintf("%sIndent", op) },
|
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 {
|
HeadToPtrHead: func() string {
|
||||||
return fmt.Sprintf(
|
return fmt.Sprintf(
|
||||||
"Struct%sFieldPtr%sHead%s%s",
|
"Struct%sFieldPtr%sHead%s%s",
|
||||||
|
@ -335,6 +373,21 @@ func (t opType) fieldToStringTagField() opType {
|
||||||
Op: op,
|
Op: op,
|
||||||
Code: "StructField",
|
Code: "StructField",
|
||||||
Indent: func() string { return fmt.Sprintf("%sIndent", op) },
|
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 },
|
HeadToPtrHead: func() string { return op },
|
||||||
HeadToNPtrHead: func() string { return op },
|
HeadToNPtrHead: func() string { return op },
|
||||||
HeadToAnonymousHead: func() string { return op },
|
HeadToAnonymousHead: func() string { return op },
|
||||||
|
@ -366,6 +419,7 @@ func (t opType) fieldToStringTagField() opType {
|
||||||
Op: fmt.Sprintf("%sIndent", typ.Op),
|
Op: fmt.Sprintf("%sIndent", typ.Op),
|
||||||
Code: typ.Code,
|
Code: typ.Code,
|
||||||
Indent: func() string { return fmt.Sprintf("%sIndent", typ.Op) },
|
Indent: func() string { return fmt.Sprintf("%sIndent", typ.Op) },
|
||||||
|
Escaped: func() string { return fmt.Sprintf("%sIndent", typ.Escaped()) },
|
||||||
HeadToPtrHead: func() string {
|
HeadToPtrHead: func() string {
|
||||||
return fmt.Sprintf("%sIndent", typ.HeadToPtrHead())
|
return fmt.Sprintf("%sIndent", typ.HeadToPtrHead())
|
||||||
},
|
},
|
||||||
|
|
11610
encode_optype.go
11610
encode_optype.go
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue