80 lines
1.7 KiB
Go
80 lines
1.7 KiB
Go
|
// generated by gotemplate
|
||
|
|
||
|
package opt
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
|
||
|
"github.com/mailru/easyjson/jlexer"
|
||
|
"github.com/mailru/easyjson/jwriter"
|
||
|
)
|
||
|
|
||
|
// template type Optional(A)
|
||
|
|
||
|
// A 'gotemplate'-based type for providing optional semantics without using pointers.
|
||
|
type String struct {
|
||
|
V string
|
||
|
Defined bool
|
||
|
}
|
||
|
|
||
|
// Creates an optional type with a given value.
|
||
|
func OString(v string) String {
|
||
|
return String{V: v, Defined: true}
|
||
|
}
|
||
|
|
||
|
// Get returns the value or given default in the case the value is undefined.
|
||
|
func (v String) Get(deflt string) string {
|
||
|
if !v.Defined {
|
||
|
return deflt
|
||
|
}
|
||
|
return v.V
|
||
|
}
|
||
|
|
||
|
// MarshalEasyJSON does JSON marshaling using easyjson interface.
|
||
|
func (v String) MarshalEasyJSON(w *jwriter.Writer) {
|
||
|
if v.Defined {
|
||
|
w.String(v.V)
|
||
|
} else {
|
||
|
w.RawString("null")
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// UnmarshalEasyJSON does JSON unmarshaling using easyjson interface.
|
||
|
func (v *String) UnmarshalEasyJSON(l *jlexer.Lexer) {
|
||
|
if l.IsNull() {
|
||
|
l.Skip()
|
||
|
*v = String{}
|
||
|
} else {
|
||
|
v.V = l.String()
|
||
|
v.Defined = true
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// MarshalJSON implements a standard json marshaler interface.
|
||
|
func (v *String) MarshalJSON() ([]byte, error) {
|
||
|
w := jwriter.Writer{}
|
||
|
v.MarshalEasyJSON(&w)
|
||
|
return w.Buffer.BuildBytes(), w.Error
|
||
|
}
|
||
|
|
||
|
// MarshalJSON implements a standard json marshaler interface.
|
||
|
func (v *String) UnmarshalJSON(data []byte) error {
|
||
|
l := jlexer.Lexer{}
|
||
|
v.UnmarshalEasyJSON(&l)
|
||
|
return l.Error()
|
||
|
}
|
||
|
|
||
|
// IsDefined returns whether the value is defined, a function is required so that it can
|
||
|
// be used in an interface.
|
||
|
func (v String) IsDefined() bool {
|
||
|
return v.Defined
|
||
|
}
|
||
|
|
||
|
// String implements a stringer interface using fmt.Sprint for the value.
|
||
|
func (v String) String() string {
|
||
|
if !v.Defined {
|
||
|
return "<undefined>"
|
||
|
}
|
||
|
return fmt.Sprint(v.V)
|
||
|
}
|