options.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. // Copyright 2023 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. package jsonopts
  5. import (
  6. "github.com/go-json-experiment/json/internal"
  7. "github.com/go-json-experiment/json/internal/jsonflags"
  8. )
  9. // Options is the common options type shared across json packages.
  10. type Options interface {
  11. // JSONOptions is exported so related json packages can implement Options.
  12. JSONOptions(internal.NotForPublicUse)
  13. }
  14. // Struct is the combination of all options in struct form.
  15. // This is efficient to pass down the call stack and to query.
  16. type Struct struct {
  17. Flags jsonflags.Flags
  18. CoderValues
  19. ArshalValues
  20. }
  21. type CoderValues struct {
  22. Indent string // jsonflags.Indent
  23. IndentPrefix string // jsonflags.IndentPrefix
  24. ByteLimit int64 // jsonflags.ByteLimit
  25. DepthLimit int // jsonflags.DepthLimit
  26. }
  27. type ArshalValues struct {
  28. // The Marshalers and Unmarshalers fields use the any type to avoid a
  29. // concrete dependency on *json.Marshalers and *json.Unmarshalers,
  30. // which would in turn create a dependency on the "reflect" package.
  31. Marshalers any // jsonflags.Marshalers
  32. Unmarshalers any // jsonflags.Unmarshalers
  33. Format string
  34. FormatDepth int
  35. }
  36. // DefaultOptionsV2 is the set of all options that define default v2 behavior.
  37. var DefaultOptionsV2 = Struct{
  38. Flags: jsonflags.Flags{
  39. Presence: uint64(jsonflags.AllFlags & ^jsonflags.WhitespaceFlags),
  40. Values: uint64(0),
  41. },
  42. }
  43. // DefaultOptionsV1 is the set of all options that define default v1 behavior.
  44. var DefaultOptionsV1 = Struct{
  45. Flags: jsonflags.Flags{
  46. Presence: uint64(jsonflags.AllFlags & ^jsonflags.WhitespaceFlags),
  47. Values: uint64(jsonflags.DefaultV1Flags),
  48. },
  49. }
  50. // CopyCoderOptions copies coder-specific options from src to dst.
  51. // This is used by json.MarshalEncode and json.UnmarshalDecode since those
  52. // functions ignore any coder-specific options and uses the options from the
  53. // Encoder or Decoder that is passed in.
  54. func (dst *Struct) CopyCoderOptions(src *Struct) {
  55. srcFlags := src.Flags
  56. srcFlags.Clear(^jsonflags.AllCoderFlags)
  57. dst.Flags.Join(srcFlags)
  58. dst.CoderValues = src.CoderValues
  59. }
  60. func (*Struct) JSONOptions(internal.NotForPublicUse) {}
  61. // GetUnknownOption is injected by the "json" package to handle Options
  62. // declared in that package so that "jsonopts" can handle them.
  63. var GetUnknownOption = func(*Struct, Options) (any, bool) { panic("unknown option") }
  64. func GetOption[T any](opts Options, setter func(T) Options) (T, bool) {
  65. // Collapse the options to *Struct to simplify lookup.
  66. structOpts, ok := opts.(*Struct)
  67. if !ok {
  68. var structOpts2 Struct
  69. structOpts2.Join(opts)
  70. structOpts = &structOpts2
  71. }
  72. // Lookup the option based on the return value of the setter.
  73. var zero T
  74. switch opt := setter(zero).(type) {
  75. case jsonflags.Bools:
  76. v := structOpts.Flags.Get(opt)
  77. ok := structOpts.Flags.Has(opt)
  78. return any(v).(T), ok
  79. case Indent:
  80. if !structOpts.Flags.Has(jsonflags.Indent) {
  81. return zero, false
  82. }
  83. return any(structOpts.Indent).(T), true
  84. case IndentPrefix:
  85. if !structOpts.Flags.Has(jsonflags.IndentPrefix) {
  86. return zero, false
  87. }
  88. return any(structOpts.IndentPrefix).(T), true
  89. case ByteLimit:
  90. if !structOpts.Flags.Has(jsonflags.ByteLimit) {
  91. return zero, false
  92. }
  93. return any(structOpts.ByteLimit).(T), true
  94. case DepthLimit:
  95. if !structOpts.Flags.Has(jsonflags.DepthLimit) {
  96. return zero, false
  97. }
  98. return any(structOpts.DepthLimit).(T), true
  99. default:
  100. v, ok := GetUnknownOption(structOpts, opt)
  101. return v.(T), ok
  102. }
  103. }
  104. // JoinUnknownOption is injected by the "json" package to handle Options
  105. // declared in that package so that "jsonopts" can handle them.
  106. var JoinUnknownOption = func(*Struct, Options) { panic("unknown option") }
  107. func (dst *Struct) Join(srcs ...Options) {
  108. for _, src := range srcs {
  109. switch src := src.(type) {
  110. case nil:
  111. continue
  112. case jsonflags.Bools:
  113. dst.Flags.Set(src)
  114. case Indent:
  115. dst.Flags.Set(jsonflags.Multiline | jsonflags.Indent | 1)
  116. dst.Indent = string(src)
  117. case IndentPrefix:
  118. dst.Flags.Set(jsonflags.Multiline | jsonflags.IndentPrefix | 1)
  119. dst.IndentPrefix = string(src)
  120. case ByteLimit:
  121. dst.Flags.Set(jsonflags.ByteLimit | 1)
  122. dst.ByteLimit = int64(src)
  123. case DepthLimit:
  124. dst.Flags.Set(jsonflags.DepthLimit | 1)
  125. dst.DepthLimit = int(src)
  126. case *Struct:
  127. dst.Flags.Join(src.Flags)
  128. if src.Flags.Has(jsonflags.NonBooleanFlags) {
  129. if src.Flags.Has(jsonflags.Indent) {
  130. dst.Indent = src.Indent
  131. }
  132. if src.Flags.Has(jsonflags.IndentPrefix) {
  133. dst.IndentPrefix = src.IndentPrefix
  134. }
  135. if src.Flags.Has(jsonflags.ByteLimit) {
  136. dst.ByteLimit = src.ByteLimit
  137. }
  138. if src.Flags.Has(jsonflags.DepthLimit) {
  139. dst.DepthLimit = src.DepthLimit
  140. }
  141. if src.Flags.Has(jsonflags.Marshalers) {
  142. dst.Marshalers = src.Marshalers
  143. }
  144. if src.Flags.Has(jsonflags.Unmarshalers) {
  145. dst.Unmarshalers = src.Unmarshalers
  146. }
  147. }
  148. if src.Format != "" {
  149. dst.Format = src.Format
  150. dst.FormatDepth = src.FormatDepth
  151. }
  152. default:
  153. JoinUnknownOption(dst, src)
  154. }
  155. }
  156. }
  157. type (
  158. Indent string // jsontext.WithIndent
  159. IndentPrefix string // jsontext.WithIndentPrefix
  160. ByteLimit int64 // jsontext.WithByteLimit
  161. DepthLimit int // jsontext.WithDepthLimit
  162. // type for jsonflags.Marshalers declared in "json" package
  163. // type for jsonflags.Unmarshalers declared in "json" package
  164. )
  165. func (Indent) JSONOptions(internal.NotForPublicUse) {}
  166. func (IndentPrefix) JSONOptions(internal.NotForPublicUse) {}
  167. func (ByteLimit) JSONOptions(internal.NotForPublicUse) {}
  168. func (DepthLimit) JSONOptions(internal.NotForPublicUse) {}