1// generated by gotemplate 2 3package opt 4 5import ( 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. 15type Float64 struct { 16 V float64 17 Defined bool 18} 19 20// Creates an optional type with a given value. 21func OFloat64(v float64) Float64 { 22 return Float64{V: v, Defined: true} 23} 24 25// Get returns the value or given default in the case the value is undefined. 26func (v Float64) Get(deflt float64) float64 { 27 if !v.Defined { 28 return deflt 29 } 30 return v.V 31} 32 33// MarshalEasyJSON does JSON marshaling using easyjson interface. 34func (v Float64) MarshalEasyJSON(w *jwriter.Writer) { 35 if v.Defined { 36 w.Float64(v.V) 37 } else { 38 w.RawString("null") 39 } 40} 41 42// UnmarshalEasyJSON does JSON unmarshaling using easyjson interface. 43func (v *Float64) UnmarshalEasyJSON(l *jlexer.Lexer) { 44 if l.IsNull() { 45 l.Skip() 46 *v = Float64{} 47 } else { 48 v.V = l.Float64() 49 v.Defined = true 50 } 51} 52 53// MarshalJSON implements a standard json marshaler interface. 54func (v Float64) 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. 61func (v *Float64) 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. 69func (v Float64) IsDefined() bool { 70 return v.Defined 71} 72 73// String implements a stringer interface using fmt.Sprint for the value. 74func (v Float64) String() string { 75 if !v.Defined { 76 return "<undefined>" 77 } 78 return fmt.Sprint(v.V) 79} 80