Fix error by linter

This commit is contained in:
Masaaki Goshima 2021-03-18 16:30:08 +09:00
parent 969dfcec31
commit 6a749c956b
10 changed files with 157 additions and 347 deletions

View File

@ -62,6 +62,10 @@ issues:
- path: rtype.go - path: rtype.go
linters: linters:
- golint - golint
- stylecheck
- path: error.go
linters:
- staticcheck
# Maximum issues count per one linter. Set to 0 to disable. Default is 50. # Maximum issues count per one linter. Set to 0 to disable. Default is 50.
max-issues-per-linter: 0 max-issues-per-linter: 0

View File

@ -7,6 +7,8 @@ import (
"strings" "strings"
"unicode" "unicode"
"unsafe" "unsafe"
"github.com/goccy/go-json/internal/runtime"
) )
var ( var (
@ -327,16 +329,16 @@ func decodeCompileStruct(typ *rtype, structName, fieldName string, structTypeToD
structName = typ.Name() structName = typ.Name()
for i := 0; i < fieldNum; i++ { for i := 0; i < fieldNum; i++ {
field := typ.Field(i) field := typ.Field(i)
if isIgnoredStructField(field) { if runtime.IsIgnoredStructField(field) {
continue continue
} }
isUnexportedField := unicode.IsLower([]rune(field.Name)[0]) isUnexportedField := unicode.IsLower([]rune(field.Name)[0])
tag := structTagFromField(field) tag := runtime.StructTagFromField(field)
dec, err := decodeCompile(type2rtype(field.Type), structName, field.Name, structTypeToDecoder) dec, err := decodeCompile(type2rtype(field.Type), structName, field.Name, structTypeToDecoder)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if field.Anonymous && !tag.isTaggedKey { if field.Anonymous && !tag.IsTaggedKey {
if stDec, ok := dec.(*structDecoder); ok { if stDec, ok := dec.(*structDecoder); ok {
if type2rtype(field.Type) == typ { if type2rtype(field.Type) == typ {
// recursive definition // recursive definition
@ -414,19 +416,19 @@ func decodeCompileStruct(typ *rtype, structName, fieldName string, structTypeToD
} }
} }
} else { } else {
if tag.isString { if tag.IsString {
dec = newWrappedStringDecoder(type2rtype(field.Type), dec, structName, field.Name) dec = newWrappedStringDecoder(type2rtype(field.Type), dec, structName, field.Name)
} }
var key string var key string
if tag.key != "" { if tag.Key != "" {
key = tag.key key = tag.Key
} else { } else {
key = field.Name key = field.Name
} }
fieldSet := &structFieldSet{ fieldSet := &structFieldSet{
dec: dec, dec: dec,
offset: field.Offset, offset: field.Offset,
isTaggedKey: tag.isTaggedKey, isTaggedKey: tag.IsTaggedKey,
key: key, key: key,
keyLen: int64(len(key)), keyLen: int64(len(key)),
} }

108
indent.go
View File

@ -1,108 +0,0 @@
package json
import "bytes"
func encodeWithIndent(dst *bytes.Buffer, src []byte, prefix, indentStr string) error {
length := int64(len(src))
indentNum := 0
indentBytes := []byte(indentStr)
for cursor := int64(0); cursor < length; cursor++ {
c := src[cursor]
switch c {
case ' ', '\t', '\n', '\r':
continue
case '"':
if err := dst.WriteByte(c); err != nil {
return err
}
for {
cursor++
if err := dst.WriteByte(src[cursor]); err != nil {
return err
}
switch src[cursor] {
case '\\':
cursor++
if err := dst.WriteByte(src[cursor]); err != nil {
return err
}
case '"':
goto LOOP_END
case nul:
return errUnexpectedEndOfJSON("string", length)
}
}
case '{':
if cursor+1 < length && src[cursor+1] == '}' {
if _, err := dst.Write([]byte{'{', '}'}); err != nil {
return err
}
cursor++
} else {
indentNum++
b := []byte{c, '\n'}
b = append(b, prefix...)
b = append(b, bytes.Repeat(indentBytes, indentNum)...)
if _, err := dst.Write(b); err != nil {
return err
}
}
case '}':
indentNum--
if indentNum < 0 {
return errInvalidCharacter('}', "}", cursor)
}
b := []byte{'\n'}
b = append(b, prefix...)
b = append(b, bytes.Repeat(indentBytes, indentNum)...)
b = append(b, c)
if _, err := dst.Write(b); err != nil {
return err
}
case '[':
if cursor+1 < length && src[cursor+1] == ']' {
if _, err := dst.Write([]byte{'[', ']'}); err != nil {
return err
}
cursor++
} else {
indentNum++
b := []byte{c, '\n'}
b = append(b, prefix...)
b = append(b, bytes.Repeat(indentBytes, indentNum)...)
if _, err := dst.Write(b); err != nil {
return err
}
}
case ']':
indentNum--
if indentNum < 0 {
return errInvalidCharacter(']', "]", cursor)
}
b := []byte{'\n'}
b = append(b, prefix...)
b = append(b, bytes.Repeat(indentBytes, indentNum)...)
b = append(b, c)
if _, err := dst.Write(b); err != nil {
return err
}
case ':':
if _, err := dst.Write([]byte{':', ' '}); err != nil {
return err
}
case ',':
b := []byte{',', '\n'}
b = append(b, prefix...)
b = append(b, bytes.Repeat(indentBytes, indentNum)...)
if _, err := dst.Write(b); err != nil {
return err
}
default:
if err := dst.WriteByte(c); err != nil {
return err
}
}
LOOP_END:
}
return nil
}

View File

@ -21,42 +21,41 @@ import (
const uintptrSize = 4 << (^uintptr(0) >> 63) const uintptrSize = 4 << (^uintptr(0) >> 63)
var ( var (
load = encoder.Load load = encoder.Load
store = encoder.Store store = encoder.Store
loadNPtr = encoder.LoadNPtr loadNPtr = encoder.LoadNPtr
ptrToPtr = encoder.PtrToPtr ptrToPtr = encoder.PtrToPtr
ptrToNPtr = encoder.PtrToNPtr ptrToNPtr = encoder.PtrToNPtr
ptrToUnsafePtr = encoder.PtrToUnsafePtr ptrToUnsafePtr = encoder.PtrToUnsafePtr
ptrToInterface = encoder.PtrToInterface ptrToInterface = encoder.PtrToInterface
ptrToUint64 = encoder.PtrToUint64 ptrToUint64 = encoder.PtrToUint64
ptrToFloat32 = encoder.PtrToFloat32 ptrToFloat32 = encoder.PtrToFloat32
ptrToFloat64 = encoder.PtrToFloat64 ptrToFloat64 = encoder.PtrToFloat64
ptrToString = encoder.PtrToString ptrToString = encoder.PtrToString
ptrToBool = encoder.PtrToBool ptrToBool = encoder.PtrToBool
ptrToBytes = encoder.PtrToBytes ptrToBytes = encoder.PtrToBytes
ptrToNumber = encoder.PtrToNumber ptrToNumber = encoder.PtrToNumber
ptrToSlice = encoder.PtrToSlice ptrToSlice = encoder.PtrToSlice
appendInt = encoder.AppendInt appendInt = encoder.AppendInt
appendUint = encoder.AppendUint appendUint = encoder.AppendUint
appendFloat32 = encoder.AppendFloat32 appendFloat32 = encoder.AppendFloat32
appendFloat64 = encoder.AppendFloat64 appendFloat64 = encoder.AppendFloat64
appendString = encoder.AppendString appendString = encoder.AppendString
appendBool = encoder.AppendBool appendBool = encoder.AppendBool
appendByteSlice = encoder.AppendByteSlice appendByteSlice = encoder.AppendByteSlice
appendNumber = encoder.AppendNumber appendNumber = encoder.AppendNumber
appendMarshalJSON = encoder.AppendMarshalJSON appendMarshalJSON = encoder.AppendMarshalJSON
appendMarshalText = encoder.AppendMarshalText appendMarshalText = encoder.AppendMarshalText
appendNull = encoder.AppendNull appendNull = encoder.AppendNull
appendComma = encoder.AppendComma appendComma = encoder.AppendComma
appendStructEnd = encoder.AppendStructEnd appendStructEnd = encoder.AppendStructEnd
errUnsupportedValue = encoder.ErrUnsupportedValue errUnsupportedValue = encoder.ErrUnsupportedValue
errUnsupportedFloat = encoder.ErrUnsupportedFloat errUnsupportedFloat = encoder.ErrUnsupportedFloat
errMarshalerWithCode = encoder.ErrMarshalerWithCode mapiterinit = encoder.MapIterInit
mapiterinit = encoder.MapIterInit mapiterkey = encoder.MapIterKey
mapiterkey = encoder.MapIterKey mapitervalue = encoder.MapIterValue
mapitervalue = encoder.MapIterValue mapiternext = encoder.MapIterNext
mapiternext = encoder.MapIterNext maplen = encoder.MapLen
maplen = encoder.MapLen
) )
type emptyInterface struct { type emptyInterface struct {

View File

@ -21,42 +21,41 @@ import (
const uintptrSize = 4 << (^uintptr(0) >> 63) const uintptrSize = 4 << (^uintptr(0) >> 63)
var ( var (
load = encoder.Load load = encoder.Load
store = encoder.Store store = encoder.Store
loadNPtr = encoder.LoadNPtr loadNPtr = encoder.LoadNPtr
ptrToPtr = encoder.PtrToPtr ptrToPtr = encoder.PtrToPtr
ptrToNPtr = encoder.PtrToNPtr ptrToNPtr = encoder.PtrToNPtr
ptrToUnsafePtr = encoder.PtrToUnsafePtr ptrToUnsafePtr = encoder.PtrToUnsafePtr
ptrToInterface = encoder.PtrToInterface ptrToInterface = encoder.PtrToInterface
ptrToUint64 = encoder.PtrToUint64 ptrToUint64 = encoder.PtrToUint64
ptrToFloat32 = encoder.PtrToFloat32 ptrToFloat32 = encoder.PtrToFloat32
ptrToFloat64 = encoder.PtrToFloat64 ptrToFloat64 = encoder.PtrToFloat64
ptrToString = encoder.PtrToString ptrToString = encoder.PtrToString
ptrToBool = encoder.PtrToBool ptrToBool = encoder.PtrToBool
ptrToBytes = encoder.PtrToBytes ptrToBytes = encoder.PtrToBytes
ptrToNumber = encoder.PtrToNumber ptrToNumber = encoder.PtrToNumber
ptrToSlice = encoder.PtrToSlice ptrToSlice = encoder.PtrToSlice
appendInt = encoder.AppendInt appendInt = encoder.AppendInt
appendUint = encoder.AppendUint appendUint = encoder.AppendUint
appendFloat32 = encoder.AppendFloat32 appendFloat32 = encoder.AppendFloat32
appendFloat64 = encoder.AppendFloat64 appendFloat64 = encoder.AppendFloat64
appendString = encoder.AppendEscapedString appendString = encoder.AppendEscapedString
appendBool = encoder.AppendBool appendBool = encoder.AppendBool
appendByteSlice = encoder.AppendByteSlice appendByteSlice = encoder.AppendByteSlice
appendNumber = encoder.AppendNumber appendNumber = encoder.AppendNumber
appendMarshalJSON = encoder.AppendMarshalJSON appendMarshalJSON = encoder.AppendMarshalJSON
appendMarshalText = encoder.AppendMarshalText appendMarshalText = encoder.AppendMarshalText
appendNull = encoder.AppendNull appendNull = encoder.AppendNull
appendComma = encoder.AppendComma appendComma = encoder.AppendComma
appendStructEnd = encoder.AppendStructEnd appendStructEnd = encoder.AppendStructEnd
errUnsupportedValue = encoder.ErrUnsupportedValue errUnsupportedValue = encoder.ErrUnsupportedValue
errUnsupportedFloat = encoder.ErrUnsupportedFloat errUnsupportedFloat = encoder.ErrUnsupportedFloat
errMarshalerWithCode = encoder.ErrMarshalerWithCode mapiterinit = encoder.MapIterInit
mapiterinit = encoder.MapIterInit mapiterkey = encoder.MapIterKey
mapiterkey = encoder.MapIterKey mapitervalue = encoder.MapIterValue
mapitervalue = encoder.MapIterValue mapiternext = encoder.MapIterNext
mapiternext = encoder.MapIterNext maplen = encoder.MapLen
maplen = encoder.MapLen
) )
type emptyInterface struct { type emptyInterface struct {

View File

@ -16,43 +16,42 @@ import (
const uintptrSize = 4 << (^uintptr(0) >> 63) const uintptrSize = 4 << (^uintptr(0) >> 63)
var ( var (
load = encoder.Load load = encoder.Load
store = encoder.Store store = encoder.Store
loadNPtr = encoder.LoadNPtr loadNPtr = encoder.LoadNPtr
ptrToPtr = encoder.PtrToPtr ptrToPtr = encoder.PtrToPtr
ptrToNPtr = encoder.PtrToNPtr ptrToNPtr = encoder.PtrToNPtr
ptrToUnsafePtr = encoder.PtrToUnsafePtr ptrToUnsafePtr = encoder.PtrToUnsafePtr
ptrToInterface = encoder.PtrToInterface ptrToInterface = encoder.PtrToInterface
ptrToUint64 = encoder.PtrToUint64 ptrToUint64 = encoder.PtrToUint64
ptrToFloat32 = encoder.PtrToFloat32 ptrToFloat32 = encoder.PtrToFloat32
ptrToFloat64 = encoder.PtrToFloat64 ptrToFloat64 = encoder.PtrToFloat64
ptrToString = encoder.PtrToString ptrToString = encoder.PtrToString
ptrToBool = encoder.PtrToBool ptrToBool = encoder.PtrToBool
ptrToBytes = encoder.PtrToBytes ptrToBytes = encoder.PtrToBytes
ptrToNumber = encoder.PtrToNumber ptrToNumber = encoder.PtrToNumber
ptrToSlice = encoder.PtrToSlice ptrToSlice = encoder.PtrToSlice
appendInt = encoder.AppendInt appendInt = encoder.AppendInt
appendUint = encoder.AppendUint appendUint = encoder.AppendUint
appendFloat32 = encoder.AppendFloat32 appendFloat32 = encoder.AppendFloat32
appendFloat64 = encoder.AppendFloat64 appendFloat64 = encoder.AppendFloat64
appendString = encoder.AppendEscapedString appendString = encoder.AppendEscapedString
appendBool = encoder.AppendBool appendBool = encoder.AppendBool
appendByteSlice = encoder.AppendByteSlice appendByteSlice = encoder.AppendByteSlice
appendNumber = encoder.AppendNumber appendNumber = encoder.AppendNumber
appendMarshalJSON = encoder.AppendMarshalJSONIndent appendMarshalJSON = encoder.AppendMarshalJSONIndent
appendMarshalText = encoder.AppendMarshalTextIndent appendMarshalText = encoder.AppendMarshalTextIndent
appendNull = encoder.AppendNull appendNull = encoder.AppendNull
appendComma = encoder.AppendCommaIndent appendComma = encoder.AppendCommaIndent
appendIndent = encoder.AppendIndent appendIndent = encoder.AppendIndent
appendStructEnd = encoder.AppendStructEndIndent appendStructEnd = encoder.AppendStructEndIndent
errUnsupportedValue = encoder.ErrUnsupportedValue errUnsupportedValue = encoder.ErrUnsupportedValue
errUnsupportedFloat = encoder.ErrUnsupportedFloat errUnsupportedFloat = encoder.ErrUnsupportedFloat
errMarshalerWithCode = encoder.ErrMarshalerWithCode mapiterinit = encoder.MapIterInit
mapiterinit = encoder.MapIterInit mapiterkey = encoder.MapIterKey
mapiterkey = encoder.MapIterKey mapitervalue = encoder.MapIterValue
mapitervalue = encoder.MapIterValue mapiternext = encoder.MapIterNext
mapiternext = encoder.MapIterNext maplen = encoder.MapLen
maplen = encoder.MapLen
) )
type emptyInterface struct { type emptyInterface struct {

View File

@ -22,43 +22,42 @@ import (
const uintptrSize = 4 << (^uintptr(0) >> 63) const uintptrSize = 4 << (^uintptr(0) >> 63)
var ( var (
load = encoder.Load load = encoder.Load
store = encoder.Store store = encoder.Store
loadNPtr = encoder.LoadNPtr loadNPtr = encoder.LoadNPtr
ptrToPtr = encoder.PtrToPtr ptrToPtr = encoder.PtrToPtr
ptrToNPtr = encoder.PtrToNPtr ptrToNPtr = encoder.PtrToNPtr
ptrToUnsafePtr = encoder.PtrToUnsafePtr ptrToUnsafePtr = encoder.PtrToUnsafePtr
ptrToInterface = encoder.PtrToInterface ptrToInterface = encoder.PtrToInterface
ptrToUint64 = encoder.PtrToUint64 ptrToUint64 = encoder.PtrToUint64
ptrToFloat32 = encoder.PtrToFloat32 ptrToFloat32 = encoder.PtrToFloat32
ptrToFloat64 = encoder.PtrToFloat64 ptrToFloat64 = encoder.PtrToFloat64
ptrToString = encoder.PtrToString ptrToString = encoder.PtrToString
ptrToBool = encoder.PtrToBool ptrToBool = encoder.PtrToBool
ptrToBytes = encoder.PtrToBytes ptrToBytes = encoder.PtrToBytes
ptrToNumber = encoder.PtrToNumber ptrToNumber = encoder.PtrToNumber
ptrToSlice = encoder.PtrToSlice ptrToSlice = encoder.PtrToSlice
appendInt = encoder.AppendInt appendInt = encoder.AppendInt
appendUint = encoder.AppendUint appendUint = encoder.AppendUint
appendFloat32 = encoder.AppendFloat32 appendFloat32 = encoder.AppendFloat32
appendFloat64 = encoder.AppendFloat64 appendFloat64 = encoder.AppendFloat64
appendString = encoder.AppendString appendString = encoder.AppendString
appendBool = encoder.AppendBool appendBool = encoder.AppendBool
appendByteSlice = encoder.AppendByteSlice appendByteSlice = encoder.AppendByteSlice
appendNumber = encoder.AppendNumber appendNumber = encoder.AppendNumber
appendMarshalJSON = encoder.AppendMarshalJSONIndent appendMarshalJSON = encoder.AppendMarshalJSONIndent
appendMarshalText = encoder.AppendMarshalTextIndent appendMarshalText = encoder.AppendMarshalTextIndent
appendNull = encoder.AppendNull appendNull = encoder.AppendNull
appendComma = encoder.AppendCommaIndent appendComma = encoder.AppendCommaIndent
appendIndent = encoder.AppendIndent appendIndent = encoder.AppendIndent
appendStructEnd = encoder.AppendStructEndIndent appendStructEnd = encoder.AppendStructEndIndent
errUnsupportedValue = encoder.ErrUnsupportedValue errUnsupportedValue = encoder.ErrUnsupportedValue
errUnsupportedFloat = encoder.ErrUnsupportedFloat errUnsupportedFloat = encoder.ErrUnsupportedFloat
errMarshalerWithCode = encoder.ErrMarshalerWithCode mapiterinit = encoder.MapIterInit
mapiterinit = encoder.MapIterInit mapiterkey = encoder.MapIterKey
mapiterkey = encoder.MapIterKey mapitervalue = encoder.MapIterValue
mapitervalue = encoder.MapIterValue mapiternext = encoder.MapIterNext
mapiternext = encoder.MapIterNext maplen = encoder.MapLen
maplen = encoder.MapLen
) )
type emptyInterface struct { type emptyInterface struct {

View File

@ -252,8 +252,9 @@ func IfaceIndir(*Type) bool
//go:noescape //go:noescape
func RType2Type(t *Type) reflect.Type func RType2Type(t *Type) reflect.Type
//go:nolint structcheck
type emptyInterface struct { type emptyInterface struct {
typ *Type _ *Type
ptr unsafe.Pointer ptr unsafe.Pointer
} }

View File

@ -18,10 +18,6 @@ func rtype_ptrTo(t *rtype) *rtype {
return runtime.PtrTo(t) return runtime.PtrTo(t)
} }
func ifaceIndir(t *rtype) bool {
return runtime.IfaceIndir(t)
}
func rtype2type(t *rtype) reflect.Type { func rtype2type(t *rtype) reflect.Type {
return runtime.RType2Type(t) return runtime.RType2Type(t)
} }

View File

@ -1,81 +0,0 @@
package json
import (
"reflect"
"strings"
"unicode"
)
func getTag(field reflect.StructField) string {
return field.Tag.Get("json")
}
func isIgnoredStructField(field reflect.StructField) bool {
if field.PkgPath != "" {
if field.Anonymous {
if !(field.Type.Kind() == reflect.Ptr && field.Type.Elem().Kind() == reflect.Struct) && field.Type.Kind() != reflect.Struct {
return true
}
} else {
// private field
return true
}
}
tag := getTag(field)
return tag == "-"
}
type structTag struct {
key string
isTaggedKey bool
isOmitEmpty bool
isString bool
field reflect.StructField
}
type structTags []*structTag
func (t structTags) existsKey(key string) bool {
for _, tt := range t {
if tt.key == key {
return true
}
}
return false
}
func isValidTag(s string) bool {
if s == "" {
return false
}
for _, c := range s {
switch {
case strings.ContainsRune("!#$%&()*+-./:<=>?@[]^_{|}~ ", c):
// Backslash and quote chars are reserved, but
// otherwise any punctuation chars are allowed
// in a tag name.
case !unicode.IsLetter(c) && !unicode.IsDigit(c):
return false
}
}
return true
}
func structTagFromField(field reflect.StructField) *structTag {
keyName := field.Name
tag := getTag(field)
st := &structTag{field: field}
opts := strings.Split(tag, ",")
if len(opts) > 0 {
if opts[0] != "" && isValidTag(opts[0]) {
keyName = opts[0]
st.isTaggedKey = true
}
}
st.key = keyName
if len(opts) > 1 {
st.isOmitEmpty = opts[1] == "omitempty"
st.isString = opts[1] == "string"
}
return st
}