You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
163 lines
3.4 KiB
163 lines
3.4 KiB
2 years ago
|
package wrapperspb
|
||
|
|
||
|
import (
|
||
|
"encoding/base64"
|
||
|
"errors"
|
||
|
"strconv"
|
||
|
)
|
||
|
|
||
|
func Double(value float64) *DoubleValue {
|
||
|
return &DoubleValue{Value: value}
|
||
|
}
|
||
|
|
||
|
func Float(value float32) *FloatValue {
|
||
|
return &FloatValue{Value: value}
|
||
|
}
|
||
|
|
||
|
func Int64(value int64) *Int64Value {
|
||
|
return &Int64Value{Value: value}
|
||
|
}
|
||
|
|
||
|
func UInt64(value uint64) *UInt64Value {
|
||
|
return &UInt64Value{Value: value}
|
||
|
}
|
||
|
|
||
|
func Int32(value int32) *Int32Value {
|
||
|
return &Int32Value{Value: value}
|
||
|
}
|
||
|
|
||
|
func UInt32(value uint32) *UInt32Value {
|
||
|
return &UInt32Value{Value: value}
|
||
|
}
|
||
|
|
||
|
func Bool(value bool) *BoolValue {
|
||
|
return &BoolValue{Value: value}
|
||
|
}
|
||
|
|
||
|
func String(value string) *StringValue {
|
||
|
return &StringValue{Value: value}
|
||
|
}
|
||
|
|
||
|
func Bytes(value []byte) *BytesValue {
|
||
|
return &BytesValue{Value: value}
|
||
|
}
|
||
|
|
||
|
func (m *DoubleValue) UnmarshalJSON(p []byte) error {
|
||
|
value, err := strconv.ParseFloat(string(p), 64)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
m.Value = value
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (m *DoubleValue) MarshalJSON() ([]byte, error) {
|
||
|
return []byte(strconv.FormatFloat(m.Value, 'f', -1, 64)), nil
|
||
|
}
|
||
|
|
||
|
func (m *FloatValue) UnmarshalJSON(p []byte) error {
|
||
|
value, err := strconv.ParseFloat(string(p), 64)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
m.Value = float32(value)
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (m *FloatValue) MarshalJSON() ([]byte, error) {
|
||
|
return []byte(strconv.FormatFloat(float64(m.Value), 'f', -1, 32)), nil
|
||
|
}
|
||
|
|
||
|
func (m *Int64Value) UnmarshalJSON(p []byte) error {
|
||
|
value, err := strconv.ParseInt(string(p), 10, 64)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
m.Value = value
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (m *Int64Value) MarshalJSON() ([]byte, error) {
|
||
|
return []byte(strconv.FormatInt(m.Value, 10)), nil
|
||
|
}
|
||
|
|
||
|
func (m *UInt64Value) UnmarshalJSON(p []byte) error {
|
||
|
value, err := strconv.ParseUint(string(p), 10, 64)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
m.Value = value
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (m *UInt64Value) MarshalJSON() ([]byte, error) {
|
||
|
return []byte(strconv.FormatUint(m.Value, 10)), nil
|
||
|
}
|
||
|
|
||
|
func (m *Int32Value) UnmarshalJSON(p []byte) error {
|
||
|
value, err := strconv.ParseInt(string(p), 10, 32)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
m.Value = int32(value)
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (m *Int32Value) MarshalJSON() ([]byte, error) {
|
||
|
return []byte(strconv.FormatInt(int64(m.Value), 10)), nil
|
||
|
}
|
||
|
|
||
|
func (m *UInt32Value) UnmarshalJSON(p []byte) error {
|
||
|
value, err := strconv.ParseUint(string(p), 10, 32)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
m.Value = uint32(value)
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (m *UInt32Value) MarshalJSON() ([]byte, error) {
|
||
|
return []byte(strconv.FormatUint(uint64(m.Value), 10)), nil
|
||
|
}
|
||
|
|
||
|
func (m *BoolValue) UnmarshalJSON(p []byte) error {
|
||
|
value, err := strconv.ParseBool(string(p))
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
m.Value = value
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (m *BoolValue) MarshalJSON() ([]byte, error) {
|
||
|
return []byte(strconv.FormatBool(m.Value)), nil
|
||
|
}
|
||
|
|
||
|
func (m *StringValue) UnmarshalJSON(p []byte) error {
|
||
|
if len(p) < 2 || p[0] != '"' || p[len(p)-1] != '"' {
|
||
|
return errors.New("invalid string value")
|
||
|
}
|
||
|
m.Value = string(p[1 : len(p)-1])
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (m *StringValue) MarshalJSON() ([]byte, error) {
|
||
|
return []byte(`"` + m.Value + `"`), nil
|
||
|
}
|
||
|
|
||
|
func (m *BytesValue) UnmarshalJSON(p []byte) error {
|
||
|
if len(p) < 2 || p[0] != '"' || p[len(p)-1] != '"' {
|
||
|
return errors.New("invalid bytes value")
|
||
|
}
|
||
|
value, err := base64.StdEncoding.DecodeString(string(p[1 : len(p)-1]))
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
m.Value = value
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (m *BytesValue) MarshalJSON() ([]byte, error) {
|
||
|
return []byte(`"` + base64.StdEncoding.EncodeToString(m.Value) + `"`), nil
|
||
|
}
|