Skip to content

Commit

Permalink
Group functions by "Uber Go Style Guide"
Browse files Browse the repository at this point in the history
Grouped functions according to "Function Grouping and Ordering" rules:
- Functions should be sorted in rough call order.
  Exported functions appears first and utility functions towards the end.
- Functions in a file should be be grouped by receiver.
  • Loading branch information
fxamacker committed Nov 17, 2019
1 parent 23d2052 commit ca1f6f1
Show file tree
Hide file tree
Showing 5 changed files with 433 additions and 433 deletions.
48 changes: 24 additions & 24 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,24 @@ type decodingStructType struct {
toArray bool
}

func getDecodingStructType(t reflect.Type) decodingStructType {
if v, _ := decodingStructTypeCache.Load(t); v != nil {
return v.(decodingStructType)
}

flds, structOptions := getFields(t)

toArray := structToArray(structOptions)

for i := 0; i < len(flds); i++ {
flds[i].isUnmarshaler = implementsUnmarshaler(flds[i].typ)
}

structType := decodingStructType{fields: flds, toArray: toArray}
decodingStructTypeCache.Store(t, structType)
return structType
}

type encodingStructType struct {
fields fields
canonicalFields fields
Expand All @@ -38,30 +56,6 @@ var (
encodingTypeCache sync.Map // map[reflect.Type]encodeFunc
)

func structToArray(tag string) bool {
s := ",toarray"
idx := strings.Index(tag, s)
return idx >= 0 && (len(tag) == idx+len(s) || tag[idx+len(s)] == ',')
}

func getDecodingStructType(t reflect.Type) decodingStructType {
if v, _ := decodingStructTypeCache.Load(t); v != nil {
return v.(decodingStructType)
}

flds, structOptions := getFields(t)

toArray := structToArray(structOptions)

for i := 0; i < len(flds); i++ {
flds[i].isUnmarshaler = implementsUnmarshaler(flds[i].typ)
}

structType := decodingStructType{fields: flds, toArray: toArray}
decodingStructTypeCache.Store(t, structType)
return structType
}

func getEncodingStructType(t reflect.Type) encodingStructType {
if v, _ := encodingStructTypeCache.Load(t); v != nil {
return v.(encodingStructType)
Expand Down Expand Up @@ -143,3 +137,9 @@ func getEncodeFunc(t reflect.Type) encodeFunc {
encodingTypeCache.Store(t, f)
return f
}

func structToArray(tag string) bool {
s := ",toarray"
idx := strings.Index(tag, s)
return idx >= 0 && (len(tag) == idx+len(s) || tag[idx+len(s)] == ',')
}
Loading

0 comments on commit ca1f6f1

Please sign in to comment.