| 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 Int struct { |
| 16 |
V int |
| 17 |
Defined bool |
| 18 |
} |
| 19 |
|
| 20 |
// Creates an optional type with a given value. |
| 21 |
func OInt(v int) Int { |
| 22 |
return Int{V: v, Defined: true} |
| 23 |
} |
| 24 |
|
| 25 |
// Get returns the value or given default in the case the value is undefined. |
| 26 |
func (v Int) Get(deflt int) int { |
| 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 Int) MarshalEasyJSON(w *jwriter.Writer) { |
| 35 |
if v.Defined { |
| 36 |
w.Int(v.V) |
| 37 |
} else { |
| 38 |
w.RawString("null") |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
// UnmarshalEasyJSON does JSON unmarshaling using easyjson interface. |
| 43 |
func (v *Int) UnmarshalEasyJSON(l *jlexer.Lexer) { |
| 44 |
if l.IsNull() { |
| 45 |
l.Skip() |
| 46 |
*v = Int{} |
| 47 |
} else { |
| 48 |
v.V = l.Int() |
| 49 |
v.Defined = true |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
// MarshalJSON implements a standard json marshaler interface. |
| 54 |
func (v Int) 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 *Int) 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 Int) IsDefined() bool { |
| 70 |
return v.Defined |
| 71 |
} |
| 72 |
|
| 73 |
// String implements a stringer interface using fmt.Sprint for the value. |
| 74 |
func (v Int) String() string { |
| 75 |
if !v.Defined { |
| 76 |
return "<undefined>" |
| 77 |
} |
| 78 |
return fmt.Sprint(v.V) |
| 79 |
} |