gotemplate_Uint64.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // generated by gotemplate
  2. package opt
  3. import (
  4. "fmt"
  5. "github.com/mailru/easyjson/jlexer"
  6. "github.com/mailru/easyjson/jwriter"
  7. )
  8. // template type Optional(A)
  9. // A 'gotemplate'-based type for providing optional semantics without using pointers.
  10. type Uint64 struct {
  11. V uint64
  12. Defined bool
  13. }
  14. // Creates an optional type with a given value.
  15. func OUint64(v uint64) Uint64 {
  16. return Uint64{V: v, Defined: true}
  17. }
  18. // Get returns the value or given default in the case the value is undefined.
  19. func (v Uint64) Get(deflt uint64) uint64 {
  20. if !v.Defined {
  21. return deflt
  22. }
  23. return v.V
  24. }
  25. // MarshalEasyJSON does JSON marshaling using easyjson interface.
  26. func (v Uint64) MarshalEasyJSON(w *jwriter.Writer) {
  27. if v.Defined {
  28. w.Uint64(v.V)
  29. } else {
  30. w.RawString("null")
  31. }
  32. }
  33. // UnmarshalEasyJSON does JSON unmarshaling using easyjson interface.
  34. func (v *Uint64) UnmarshalEasyJSON(l *jlexer.Lexer) {
  35. if l.IsNull() {
  36. l.Skip()
  37. *v = Uint64{}
  38. } else {
  39. v.V = l.Uint64()
  40. v.Defined = true
  41. }
  42. }
  43. // MarshalJSON implements a standard json marshaler interface.
  44. func (v Uint64) MarshalJSON() ([]byte, error) {
  45. w := jwriter.Writer{}
  46. v.MarshalEasyJSON(&w)
  47. return w.Buffer.BuildBytes(), w.Error
  48. }
  49. // UnmarshalJSON implements a standard json unmarshaler interface.
  50. func (v *Uint64) UnmarshalJSON(data []byte) error {
  51. l := jlexer.Lexer{Data: data}
  52. v.UnmarshalEasyJSON(&l)
  53. return l.Error()
  54. }
  55. // IsDefined returns whether the value is defined, a function is required so that it can
  56. // be used in an interface.
  57. func (v Uint64) IsDefined() bool {
  58. return v.Defined
  59. }
  60. // String implements a stringer interface using fmt.Sprint for the value.
  61. func (v Uint64) String() string {
  62. if !v.Defined {
  63. return "<undefined>"
  64. }
  65. return fmt.Sprint(v.V)
  66. }