You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

265 lines
5.6 KiB

  1. package _generated
  2. import (
  3. "os"
  4. "time"
  5. "github.com/tinylib/msgp/msgp"
  6. )
  7. //go:generate msgp -o generated.go
  8. // All of the struct
  9. // definitions in this
  10. // file are fed to the code
  11. // generator when `make test` is
  12. // called, followed by an
  13. // invocation of `go test -v` in this
  14. // directory. A simple way of testing
  15. // a struct definition is
  16. // by adding it to this file.
  17. type Block [32]byte
  18. // tests edge-cases with
  19. // compiling size compilation.
  20. type X struct {
  21. Values [32]byte // should compile to 32*msgp.ByteSize; encoded as Bin
  22. ValuesPtr *[32]byte // check (*)[:] deref
  23. More Block // should be identical to the above
  24. Others [][32]int32 // should compile to len(x.Others)*32*msgp.Int32Size
  25. Matrix [][]int32 // should not optimize
  26. ManyFixed []Fixed
  27. }
  28. // test fixed-size struct
  29. // size compilation
  30. type Fixed struct {
  31. A float64
  32. B bool
  33. }
  34. type TestType struct {
  35. F *float64 `msg:"float"`
  36. Els map[string]string `msg:"elements"`
  37. Obj struct { // test anonymous struct
  38. ValueA string `msg:"value_a"`
  39. ValueB []byte `msg:"value_b"`
  40. } `msg:"object"`
  41. Child *TestType `msg:"child"`
  42. Time time.Time `msg:"time"`
  43. Any interface{} `msg:"any"`
  44. Appended msgp.Raw `msg:"appended"`
  45. Num msgp.Number `msg:"num"`
  46. Byte byte
  47. Rune rune
  48. RunePtr *rune
  49. RunePtrPtr **rune
  50. RuneSlice []rune
  51. Slice1 []string
  52. Slice2 []string
  53. SlicePtr *[]string
  54. }
  55. //msgp:tuple Object
  56. type Object struct {
  57. ObjectNo string `msg:"objno"`
  58. Slice1 []string `msg:"slice1"`
  59. Slice2 []string `msg:"slice2"`
  60. MapMap map[string]map[string]string
  61. }
  62. //msgp:tuple TestBench
  63. type TestBench struct {
  64. Name string
  65. BirthDay time.Time
  66. Phone string
  67. Siblings int
  68. Spouse bool
  69. Money float64
  70. }
  71. //msgp:tuple TestFast
  72. type TestFast struct {
  73. Lat, Long, Alt float64 // test inline decl
  74. Data []byte
  75. }
  76. // Test nested aliases
  77. type FastAlias TestFast
  78. type AliasContainer struct {
  79. Fast FastAlias
  80. }
  81. // Test dependency resolution
  82. type IntA int
  83. type IntB IntA
  84. type IntC IntB
  85. type TestHidden struct {
  86. A string
  87. B []float64
  88. Bad func(string) bool // This results in a warning: field "Bad" unsupported
  89. }
  90. type Embedded struct {
  91. *Embedded // test embedded field
  92. Children []Embedded
  93. PtrChildren []*Embedded
  94. Other string
  95. }
  96. const eight = 8
  97. type Things struct {
  98. Cmplx complex64 `msg:"complex"` // test slices
  99. Vals []int32 `msg:"values"`
  100. Arr [msgp.ExtensionPrefixSize]float64 `msg:"arr"` // test const array and *ast.SelectorExpr as array size
  101. Arr2 [4]float64 `msg:"arr2"` // test basic lit array
  102. Ext *msgp.RawExtension `msg:"ext,extension"` // test extension
  103. Oext msgp.RawExtension `msg:"oext,extension"` // test extension reference
  104. }
  105. //msgp:shim SpecialID as:[]byte using:toBytes/fromBytes
  106. type SpecialID string
  107. type TestObj struct{ ID1, ID2 SpecialID }
  108. func toBytes(id SpecialID) []byte { return []byte(string(id)) }
  109. func fromBytes(id []byte) SpecialID { return SpecialID(string(id)) }
  110. type MyEnum byte
  111. const (
  112. A MyEnum = iota
  113. B
  114. C
  115. D
  116. invalid
  117. )
  118. // test shim directive (below)
  119. //msgp:shim MyEnum as:string using:(MyEnum).String/myenumStr
  120. //msgp:shim *os.File as:string using:filetostr/filefromstr
  121. func filetostr(f *os.File) string {
  122. return f.Name()
  123. }
  124. func filefromstr(s string) *os.File {
  125. f, _ := os.Open(s)
  126. return f
  127. }
  128. func (m MyEnum) String() string {
  129. switch m {
  130. case A:
  131. return "A"
  132. case B:
  133. return "B"
  134. case C:
  135. return "C"
  136. case D:
  137. return "D"
  138. default:
  139. return "<invalid>"
  140. }
  141. }
  142. func myenumStr(s string) MyEnum {
  143. switch s {
  144. case "A":
  145. return A
  146. case "B":
  147. return B
  148. case "C":
  149. return C
  150. case "D":
  151. return D
  152. default:
  153. return invalid
  154. }
  155. }
  156. // test pass-specific directive
  157. //msgp:decode ignore Insane
  158. type Insane [3]map[string]struct{ A, B CustomInt }
  159. type Custom struct {
  160. Bts CustomBytes `msg:"bts"`
  161. Mp map[string]*Embedded `msg:"mp"`
  162. Enums []MyEnum `msg:"enums"` // test explicit enum shim
  163. Some FileHandle `msg:file_handle`
  164. }
  165. type Files []*os.File
  166. type FileHandle struct {
  167. Relevant Files `msg:"files"`
  168. Name string `msg:"name"`
  169. }
  170. type CustomInt int
  171. type CustomBytes []byte
  172. type Wrapper struct {
  173. Tree *Tree
  174. }
  175. type Tree struct {
  176. Children []Tree
  177. Element int
  178. Parent *Wrapper
  179. }
  180. // Ensure all different widths of integer can be used as constant keys.
  181. const (
  182. ConstantInt int = 8
  183. ConstantInt8 int8 = 8
  184. ConstantInt16 int16 = 8
  185. ConstantInt32 int32 = 8
  186. ConstantInt64 int64 = 8
  187. ConstantUint uint = 8
  188. ConstantUint8 uint8 = 8
  189. ConstantUint16 uint16 = 8
  190. ConstantUint32 uint32 = 8
  191. ConstantUint64 uint64 = 8
  192. )
  193. type ArrayConstants struct {
  194. ConstantInt [ConstantInt]string
  195. ConstantInt8 [ConstantInt8]string
  196. ConstantInt16 [ConstantInt16]string
  197. ConstantInt32 [ConstantInt32]string
  198. ConstantInt64 [ConstantInt64]string
  199. ConstantUint [ConstantUint]string
  200. ConstantUint8 [ConstantUint8]string
  201. ConstantUint16 [ConstantUint16]string
  202. ConstantUint32 [ConstantUint32]string
  203. ConstantUint64 [ConstantUint64]string
  204. ConstantHex [0x16]string
  205. ConstantOctal [07]string
  206. }
  207. // Ensure non-msg struct tags work:
  208. // https://github.com/tinylib/msgp/issues/201
  209. type NonMsgStructTags struct {
  210. A []string `json:"fooJSON" msg:"fooMsgp"`
  211. B string `json:"barJSON"`
  212. C []string `json:"bazJSON" msg:"-"`
  213. Nested []struct {
  214. A []string `json:"a"`
  215. B string `json:"b"`
  216. C []string `json:"c"`
  217. VeryNested []struct {
  218. A []string `json:"a"`
  219. B []string `msg:"bbbb" xml:"-"`
  220. }
  221. }
  222. }