encode.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. // Copyright 2010 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // This file contains a modified copy of the encoding/json encoder.
  5. // All dynamic behavior has been removed, and reflecttion has been replaced with go/types.
  6. // This allows us to statically find unmarshable types
  7. // with the same rules for tags, shadowing and addressability as encoding/json.
  8. // This is used for SA1026.
  9. package fakejson
  10. import (
  11. "go/token"
  12. "go/types"
  13. "sort"
  14. "strings"
  15. "unicode"
  16. "golang.org/x/exp/typeparams"
  17. "honnef.co/go/tools/staticcheck/fakereflect"
  18. )
  19. // parseTag splits a struct field's json tag into its name and
  20. // comma-separated options.
  21. func parseTag(tag string) string {
  22. if idx := strings.Index(tag, ","); idx != -1 {
  23. return tag[:idx]
  24. }
  25. return tag
  26. }
  27. func Marshal(v types.Type) *UnsupportedTypeError {
  28. enc := encoder{
  29. seen: map[fakereflect.TypeAndCanAddr]struct{}{},
  30. }
  31. return enc.newTypeEncoder(fakereflect.TypeAndCanAddr{Type: v}, "x")
  32. }
  33. // An UnsupportedTypeError is returned by Marshal when attempting
  34. // to encode an unsupported value type.
  35. type UnsupportedTypeError struct {
  36. Type types.Type
  37. Path string
  38. }
  39. var marshalerType = types.NewInterfaceType([]*types.Func{
  40. types.NewFunc(token.NoPos, nil, "MarshalJSON", types.NewSignature(nil,
  41. types.NewTuple(),
  42. types.NewTuple(
  43. types.NewVar(token.NoPos, nil, "", types.NewSlice(types.Typ[types.Byte])),
  44. types.NewVar(0, nil, "", types.Universe.Lookup("error").Type())),
  45. false,
  46. )),
  47. }, nil).Complete()
  48. var textMarshalerType = types.NewInterfaceType([]*types.Func{
  49. types.NewFunc(token.NoPos, nil, "MarshalText", types.NewSignature(nil,
  50. types.NewTuple(),
  51. types.NewTuple(
  52. types.NewVar(token.NoPos, nil, "", types.NewSlice(types.Typ[types.Byte])),
  53. types.NewVar(0, nil, "", types.Universe.Lookup("error").Type())),
  54. false,
  55. )),
  56. }, nil).Complete()
  57. type encoder struct {
  58. seen map[fakereflect.TypeAndCanAddr]struct{}
  59. }
  60. func (enc *encoder) newTypeEncoder(t fakereflect.TypeAndCanAddr, stack string) *UnsupportedTypeError {
  61. if _, ok := enc.seen[t]; ok {
  62. return nil
  63. }
  64. enc.seen[t] = struct{}{}
  65. if t.Implements(marshalerType) {
  66. return nil
  67. }
  68. if !t.IsPtr() && t.CanAddr() && fakereflect.PtrTo(t).Implements(marshalerType) {
  69. return nil
  70. }
  71. if t.Implements(textMarshalerType) {
  72. return nil
  73. }
  74. if !t.IsPtr() && t.CanAddr() && fakereflect.PtrTo(t).Implements(textMarshalerType) {
  75. return nil
  76. }
  77. switch t.Type.Underlying().(type) {
  78. case *types.Basic, *types.Interface:
  79. return nil
  80. case *types.Struct:
  81. return enc.typeFields(t, stack)
  82. case *types.Map:
  83. return enc.newMapEncoder(t, stack)
  84. case *types.Slice:
  85. return enc.newSliceEncoder(t, stack)
  86. case *types.Array:
  87. return enc.newArrayEncoder(t, stack)
  88. case *types.Pointer:
  89. // we don't have to express the pointer dereference in the path; x.f is syntactic sugar for (*x).f
  90. return enc.newTypeEncoder(t.Elem(), stack)
  91. default:
  92. return &UnsupportedTypeError{t.Type, stack}
  93. }
  94. }
  95. func (enc *encoder) newMapEncoder(t fakereflect.TypeAndCanAddr, stack string) *UnsupportedTypeError {
  96. if typeparams.IsTypeParam(t.Key().Type) {
  97. // We don't know enough about the concrete instantiation to say much about the key. The only time we could make
  98. // a definite "this key is bad" statement is if the type parameter is constrained by type terms, none of which
  99. // are tilde terms, none of which are a basic type. In all other cases, the key might implement TextMarshaler.
  100. // It doesn't seem worth checking for that one single case.
  101. return enc.newTypeEncoder(t.Elem(), stack+"[k]")
  102. }
  103. switch t.Key().Type.Underlying().(type) {
  104. case *types.Basic:
  105. default:
  106. if !t.Key().Implements(textMarshalerType) {
  107. return &UnsupportedTypeError{
  108. Type: t.Type,
  109. Path: stack,
  110. }
  111. }
  112. }
  113. return enc.newTypeEncoder(t.Elem(), stack+"[k]")
  114. }
  115. func (enc *encoder) newSliceEncoder(t fakereflect.TypeAndCanAddr, stack string) *UnsupportedTypeError {
  116. // Byte slices get special treatment; arrays don't.
  117. basic, ok := t.Elem().Type.Underlying().(*types.Basic)
  118. if ok && basic.Kind() == types.Uint8 {
  119. p := fakereflect.PtrTo(t.Elem())
  120. if !p.Implements(marshalerType) && !p.Implements(textMarshalerType) {
  121. return nil
  122. }
  123. }
  124. return enc.newArrayEncoder(t, stack)
  125. }
  126. func (enc *encoder) newArrayEncoder(t fakereflect.TypeAndCanAddr, stack string) *UnsupportedTypeError {
  127. return enc.newTypeEncoder(t.Elem(), stack+"[0]")
  128. }
  129. func isValidTag(s string) bool {
  130. if s == "" {
  131. return false
  132. }
  133. for _, c := range s {
  134. switch {
  135. case strings.ContainsRune("!#$%&()*+-./:;<=>?@[]^_{|}~ ", c):
  136. // Backslash and quote chars are reserved, but
  137. // otherwise any punctuation chars are allowed
  138. // in a tag name.
  139. case !unicode.IsLetter(c) && !unicode.IsDigit(c):
  140. return false
  141. }
  142. }
  143. return true
  144. }
  145. func typeByIndex(t fakereflect.TypeAndCanAddr, index []int) fakereflect.TypeAndCanAddr {
  146. for _, i := range index {
  147. if t.IsPtr() {
  148. t = t.Elem()
  149. }
  150. t = t.Field(i).Type
  151. }
  152. return t
  153. }
  154. func pathByIndex(t fakereflect.TypeAndCanAddr, index []int) string {
  155. path := ""
  156. for _, i := range index {
  157. if t.IsPtr() {
  158. t = t.Elem()
  159. }
  160. path += "." + t.Field(i).Name
  161. t = t.Field(i).Type
  162. }
  163. return path
  164. }
  165. // A field represents a single field found in a struct.
  166. type field struct {
  167. name string
  168. tag bool
  169. index []int
  170. typ fakereflect.TypeAndCanAddr
  171. }
  172. // byIndex sorts field by index sequence.
  173. type byIndex []field
  174. func (x byIndex) Len() int { return len(x) }
  175. func (x byIndex) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
  176. func (x byIndex) Less(i, j int) bool {
  177. for k, xik := range x[i].index {
  178. if k >= len(x[j].index) {
  179. return false
  180. }
  181. if xik != x[j].index[k] {
  182. return xik < x[j].index[k]
  183. }
  184. }
  185. return len(x[i].index) < len(x[j].index)
  186. }
  187. // typeFields returns a list of fields that JSON should recognize for the given type.
  188. // The algorithm is breadth-first search over the set of structs to include - the top struct
  189. // and then any reachable anonymous structs.
  190. func (enc *encoder) typeFields(t fakereflect.TypeAndCanAddr, stack string) *UnsupportedTypeError {
  191. // Anonymous fields to explore at the current level and the next.
  192. current := []field{}
  193. next := []field{{typ: t}}
  194. // Count of queued names for current level and the next.
  195. var count, nextCount map[fakereflect.TypeAndCanAddr]int
  196. // Types already visited at an earlier level.
  197. visited := map[fakereflect.TypeAndCanAddr]bool{}
  198. // Fields found.
  199. var fields []field
  200. for len(next) > 0 {
  201. current, next = next, current[:0]
  202. count, nextCount = nextCount, map[fakereflect.TypeAndCanAddr]int{}
  203. for _, f := range current {
  204. if visited[f.typ] {
  205. continue
  206. }
  207. visited[f.typ] = true
  208. // Scan f.typ for fields to include.
  209. for i := 0; i < f.typ.NumField(); i++ {
  210. sf := f.typ.Field(i)
  211. if sf.Anonymous {
  212. t := sf.Type
  213. if t.IsPtr() {
  214. t = t.Elem()
  215. }
  216. if !sf.IsExported() && !t.IsStruct() {
  217. // Ignore embedded fields of unexported non-struct types.
  218. continue
  219. }
  220. // Do not ignore embedded fields of unexported struct types
  221. // since they may have exported fields.
  222. } else if !sf.IsExported() {
  223. // Ignore unexported non-embedded fields.
  224. continue
  225. }
  226. tag := sf.Tag.Get("json")
  227. if tag == "-" {
  228. continue
  229. }
  230. name := parseTag(tag)
  231. if !isValidTag(name) {
  232. name = ""
  233. }
  234. index := make([]int, len(f.index)+1)
  235. copy(index, f.index)
  236. index[len(f.index)] = i
  237. ft := sf.Type
  238. if ft.Name() == "" && ft.IsPtr() {
  239. // Follow pointer.
  240. ft = ft.Elem()
  241. }
  242. // Record found field and index sequence.
  243. if name != "" || !sf.Anonymous || !ft.IsStruct() {
  244. tagged := name != ""
  245. if name == "" {
  246. name = sf.Name
  247. }
  248. field := field{
  249. name: name,
  250. tag: tagged,
  251. index: index,
  252. typ: ft,
  253. }
  254. fields = append(fields, field)
  255. if count[f.typ] > 1 {
  256. // If there were multiple instances, add a second,
  257. // so that the annihilation code will see a duplicate.
  258. // It only cares about the distinction between 1 or 2,
  259. // so don't bother generating any more copies.
  260. fields = append(fields, fields[len(fields)-1])
  261. }
  262. continue
  263. }
  264. // Record new anonymous struct to explore in next round.
  265. nextCount[ft]++
  266. if nextCount[ft] == 1 {
  267. next = append(next, field{name: ft.Name(), index: index, typ: ft})
  268. }
  269. }
  270. }
  271. }
  272. sort.Slice(fields, func(i, j int) bool {
  273. x := fields
  274. // sort field by name, breaking ties with depth, then
  275. // breaking ties with "name came from json tag", then
  276. // breaking ties with index sequence.
  277. if x[i].name != x[j].name {
  278. return x[i].name < x[j].name
  279. }
  280. if len(x[i].index) != len(x[j].index) {
  281. return len(x[i].index) < len(x[j].index)
  282. }
  283. if x[i].tag != x[j].tag {
  284. return x[i].tag
  285. }
  286. return byIndex(x).Less(i, j)
  287. })
  288. // Delete all fields that are hidden by the Go rules for embedded fields,
  289. // except that fields with JSON tags are promoted.
  290. // The fields are sorted in primary order of name, secondary order
  291. // of field index length. Loop over names; for each name, delete
  292. // hidden fields by choosing the one dominant field that survives.
  293. out := fields[:0]
  294. for advance, i := 0, 0; i < len(fields); i += advance {
  295. // One iteration per name.
  296. // Find the sequence of fields with the name of this first field.
  297. fi := fields[i]
  298. name := fi.name
  299. for advance = 1; i+advance < len(fields); advance++ {
  300. fj := fields[i+advance]
  301. if fj.name != name {
  302. break
  303. }
  304. }
  305. if advance == 1 { // Only one field with this name
  306. out = append(out, fi)
  307. continue
  308. }
  309. dominant, ok := dominantField(fields[i : i+advance])
  310. if ok {
  311. out = append(out, dominant)
  312. }
  313. }
  314. fields = out
  315. sort.Sort(byIndex(fields))
  316. for i := range fields {
  317. f := &fields[i]
  318. err := enc.newTypeEncoder(typeByIndex(t, f.index), stack+pathByIndex(t, f.index))
  319. if err != nil {
  320. return err
  321. }
  322. }
  323. return nil
  324. }
  325. // dominantField looks through the fields, all of which are known to
  326. // have the same name, to find the single field that dominates the
  327. // others using Go's embedding rules, modified by the presence of
  328. // JSON tags. If there are multiple top-level fields, the boolean
  329. // will be false: This condition is an error in Go and we skip all
  330. // the fields.
  331. func dominantField(fields []field) (field, bool) {
  332. // The fields are sorted in increasing index-length order, then by presence of tag.
  333. // That means that the first field is the dominant one. We need only check
  334. // for error cases: two fields at top level, either both tagged or neither tagged.
  335. if len(fields) > 1 && len(fields[0].index) == len(fields[1].index) && fields[0].tag == fields[1].tag {
  336. return field{}, false
  337. }
  338. return fields[0], true
  339. }