forked from mirror/go-json
Fix pointer fields
This commit is contained in:
parent
66b8cb792a
commit
9351afdb88
|
@ -277,6 +277,7 @@ func (t opType) fieldToStringTagField() opType {
|
|||
"StructFieldPtrAnonymousHeadOmitEmpty",
|
||||
"StructFieldPtrAnonymousHeadStringTag",
|
||||
"StructField",
|
||||
"StructFieldPtr",
|
||||
"StructFieldOmitEmpty",
|
||||
"StructFieldStringTag",
|
||||
} {
|
||||
|
|
2542
decode_test.go
2542
decode_test.go
File diff suppressed because it is too large
Load Diff
|
@ -481,8 +481,39 @@ func (e *Encoder) typeToHeaderType(op opType) opType {
|
|||
return opStructFieldHead
|
||||
}
|
||||
|
||||
func (e *Encoder) typeToFieldType(op opType) opType {
|
||||
switch op {
|
||||
func (e *Encoder) typeToFieldType(code *opcode) opType {
|
||||
switch code.op {
|
||||
case opPtr:
|
||||
switch code.next.op {
|
||||
case opInt:
|
||||
return opStructFieldPtrInt
|
||||
case opInt8:
|
||||
return opStructFieldPtrInt8
|
||||
case opInt16:
|
||||
return opStructFieldPtrInt16
|
||||
case opInt32:
|
||||
return opStructFieldPtrInt32
|
||||
case opInt64:
|
||||
return opStructFieldPtrInt64
|
||||
case opUint:
|
||||
return opStructFieldPtrUint
|
||||
case opUint8:
|
||||
return opStructFieldPtrUint8
|
||||
case opUint16:
|
||||
return opStructFieldPtrUint16
|
||||
case opUint32:
|
||||
return opStructFieldPtrUint32
|
||||
case opUint64:
|
||||
return opStructFieldPtrUint64
|
||||
case opFloat32:
|
||||
return opStructFieldPtrFloat32
|
||||
case opFloat64:
|
||||
return opStructFieldPtrFloat64
|
||||
case opString:
|
||||
return opStructFieldPtrString
|
||||
case opBool:
|
||||
return opStructFieldPtrBool
|
||||
}
|
||||
case opInt:
|
||||
return opStructFieldInt
|
||||
case opInt8:
|
||||
|
@ -553,8 +584,8 @@ func (e *Encoder) optimizeStructHeader(op opType, tag *structTag, withIndent boo
|
|||
return headType
|
||||
}
|
||||
|
||||
func (e *Encoder) optimizeStructField(op opType, tag *structTag, withIndent bool) opType {
|
||||
fieldType := e.typeToFieldType(op)
|
||||
func (e *Encoder) optimizeStructField(code *opcode, tag *structTag, withIndent bool) opType {
|
||||
fieldType := e.typeToFieldType(code)
|
||||
switch {
|
||||
case tag.isOmitEmpty:
|
||||
fieldType = fieldType.fieldToOmitEmptyField()
|
||||
|
@ -627,7 +658,7 @@ func (e *Encoder) structHeader(ctx *encodeCompileContext, fieldCode *opcode, val
|
|||
|
||||
func (e *Encoder) structField(ctx *encodeCompileContext, fieldCode *opcode, valueCode *opcode, tag *structTag) *opcode {
|
||||
code := (*opcode)(unsafe.Pointer(fieldCode))
|
||||
op := e.optimizeStructField(valueCode.op, tag, ctx.withIndent)
|
||||
op := e.optimizeStructField(valueCode, tag, ctx.withIndent)
|
||||
fieldCode.op = op
|
||||
switch op {
|
||||
case opStructField,
|
||||
|
|
1172
encode_optype.go
1172
encode_optype.go
File diff suppressed because it is too large
Load Diff
479
encode_vm.go
479
encode_vm.go
File diff suppressed because it is too large
Load Diff
|
@ -15,19 +15,19 @@ import (
|
|||
type Size int
|
||||
|
||||
const (
|
||||
Unrecognized Size = iota
|
||||
Small
|
||||
Large
|
||||
unrecognized Size = iota
|
||||
small
|
||||
large
|
||||
)
|
||||
|
||||
func (s *Size) UnmarshalText(text []byte) error {
|
||||
switch strings.ToLower(string(text)) {
|
||||
default:
|
||||
*s = Unrecognized
|
||||
*s = unrecognized
|
||||
case "small":
|
||||
*s = Small
|
||||
*s = small
|
||||
case "large":
|
||||
*s = Large
|
||||
*s = large
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@ -37,9 +37,9 @@ func (s Size) MarshalText() ([]byte, error) {
|
|||
switch s {
|
||||
default:
|
||||
name = "unrecognized"
|
||||
case Small:
|
||||
case small:
|
||||
name = "small"
|
||||
case Large:
|
||||
case large:
|
||||
name = "large"
|
||||
}
|
||||
return []byte(name), nil
|
||||
|
@ -58,7 +58,7 @@ func Example_textMarshalJSON() {
|
|||
}
|
||||
|
||||
fmt.Printf("Inventory Counts:\n* Small: %d\n* Large: %d\n* Unrecognized: %d\n",
|
||||
counts[Small], counts[Large], counts[Unrecognized])
|
||||
counts[small], counts[large], counts[unrecognized])
|
||||
|
||||
// Output:
|
||||
// Inventory Counts:
|
||||
|
|
Loading…
Reference in New Issue