...

Source file src/github.com/mailru/easyjson/opt/gotemplate_Uint64.go

Documentation: github.com/mailru/easyjson/opt

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

View as plain text