123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- package value
- import (
- "fmt"
- "strings"
- )
- type Value struct {
-
- FloatValue *Float
- IntValue *Int
- StringValue *String
- BooleanValue *Boolean
- ListValue *List
- MapValue *Map
- Null bool
- }
- type Int int64
- type Float float64
- type String string
- type Boolean bool
- type Field struct {
- Name string
- Value Value
- }
- type List struct {
- Items []Value
- }
- type Map struct {
- Items []Field
-
-
- index map[string]*Field
- }
- func (m *Map) Get(key string) (*Field, bool) {
- if m.index == nil {
- m.index = map[string]*Field{}
- for i := range m.Items {
- f := &m.Items[i]
- m.index[f.Name] = f
- }
- }
- f, ok := m.index[key]
- return f, ok
- }
- func (m *Map) Set(key string, value Value) {
- if f, ok := m.Get(key); ok {
- f.Value = value
- return
- }
- m.Items = append(m.Items, Field{Name: key, Value: value})
- m.index = nil
- }
- func StringValue(s string) Value {
- s2 := String(s)
- return Value{StringValue: &s2}
- }
- func IntValue(i int) Value {
- i2 := Int(i)
- return Value{IntValue: &i2}
- }
- func FloatValue(f float64) Value {
- f2 := Float(f)
- return Value{FloatValue: &f2}
- }
- func BooleanValue(b bool) Value {
- b2 := Boolean(b)
- return Value{BooleanValue: &b2}
- }
- func (v Value) String() string {
- switch {
- case v.FloatValue != nil:
- return fmt.Sprintf("%v", *v.FloatValue)
- case v.IntValue != nil:
- return fmt.Sprintf("%v", *v.IntValue)
- case v.StringValue != nil:
- return fmt.Sprintf("%q", *v.StringValue)
- case v.BooleanValue != nil:
- return fmt.Sprintf("%v", *v.BooleanValue)
- case v.ListValue != nil:
- strs := []string{}
- for _, item := range v.ListValue.Items {
- strs = append(strs, item.String())
- }
- return "[" + strings.Join(strs, ",") + "]"
- case v.MapValue != nil:
- strs := []string{}
- for _, i := range v.MapValue.Items {
- strs = append(strs, fmt.Sprintf("%v=%v", i.Name, i.Value))
- }
- return "{" + strings.Join(strs, ";") + "}"
- default:
- fallthrough
- case v.Null == true:
- return "null"
- }
- }
|