mammoth2-test.go.tmpl 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // +build !codec.notmammoth
  2. // Copyright (c) 2012-2020 Ugorji Nwoke. All rights reserved.
  3. // Use of this source code is governed by a MIT license found in the LICENSE file.
  4. // Code generated from mammoth2-test.go.tmpl - DO NOT EDIT.
  5. package codec
  6. // Increase codecoverage by covering all the codecgen paths, in fast-path and gen-helper.go....
  7. //
  8. // Note: even though this is built based on fast-path and gen-helper, we will run these tests
  9. // in all modes, including notfastpath, etc.
  10. //
  11. // Add test file for creating a mammoth generated file as _mammoth_generated.go
  12. // - generate a second mammoth files in a different file: mammoth2_generated_test.go
  13. // mammoth-test.go.tmpl will do this
  14. // - run codecgen on it, into mammoth2_codecgen_generated_test.go (no build tags)
  15. // - as part of TestMammoth, run it also
  16. // - this will cover all the codecgen, gen-helper, etc in one full run
  17. // - check in mammoth* files into github also
  18. //
  19. // Now, add some types:
  20. // - some that implement BinaryMarshal, TextMarshal, JSONMarshal, and one that implements none of it
  21. // - create a wrapper type that includes TestMammoth2, with it in slices, and maps, and the custom types
  22. // - this wrapper object is what we work encode/decode (so that the codecgen methods are called)
  23. // import "encoding/binary"
  24. import "fmt"
  25. type TestMammoth2 struct {
  26. {{range .Values }}{{if .Primitive }}{{/*
  27. */}}{{ .MethodNamePfx "F" true }} {{ .Primitive }}
  28. {{ .MethodNamePfx "Fptr" true }} *{{ .Primitive }}
  29. {{end}}{{end}}
  30. {{range .Values }}{{if not .Primitive }}{{if not .MapKey }}{{/*
  31. */}}{{ .MethodNamePfx "F" false }} []{{ .Elem }}
  32. {{ .MethodNamePfx "Fptr" false }} *[]{{ .Elem }}
  33. {{end}}{{end}}{{end}}
  34. {{range .Values }}{{if not .Primitive }}{{if .MapKey }}{{/*
  35. */}}{{ .MethodNamePfx "F" false }} map[{{ .MapKey }}]{{ .Elem }}
  36. {{ .MethodNamePfx "Fptr" false }} *map[{{ .MapKey }}]{{ .Elem }}
  37. {{end}}{{end}}{{end}}
  38. }
  39. // -----------
  40. type testMammoth2Binary uint64
  41. func (x testMammoth2Binary) MarshalBinary() (data []byte, err error) {
  42. data = make([]byte, 8)
  43. bigenstd.PutUint64(data, uint64(x))
  44. return
  45. }
  46. func (x *testMammoth2Binary) UnmarshalBinary(data []byte) (err error) {
  47. *x = testMammoth2Binary(bigenstd.Uint64(data))
  48. return
  49. }
  50. type testMammoth2Text uint64
  51. func (x testMammoth2Text) MarshalText() (data []byte, err error) {
  52. data = []byte(fmt.Sprintf("%b", uint64(x)))
  53. return
  54. }
  55. func (x *testMammoth2Text) UnmarshalText(data []byte) (err error) {
  56. _, err = fmt.Sscanf(string(data), "%b", (*uint64)(x))
  57. return
  58. }
  59. type testMammoth2Json uint64
  60. func (x testMammoth2Json) MarshalJSON() (data []byte, err error) {
  61. data = []byte(fmt.Sprintf("%v", uint64(x)))
  62. return
  63. }
  64. func (x *testMammoth2Json) UnmarshalJSON(data []byte) (err error) {
  65. _, err = fmt.Sscanf(string(data), "%v", (*uint64)(x))
  66. return
  67. }
  68. type testMammoth2Basic [4]uint64
  69. type TestMammoth2Wrapper struct {
  70. V TestMammoth2
  71. T testMammoth2Text
  72. B testMammoth2Binary
  73. J testMammoth2Json
  74. C testMammoth2Basic
  75. M map[testMammoth2Basic]TestMammoth2
  76. L []TestMammoth2
  77. A [4]int64
  78. Tcomplex128 complex128
  79. Tcomplex64 complex64
  80. Tbytes []uint8
  81. Tpbytes *[]uint8
  82. }