github.com/annwntech/go-micro/v2@v2.9.5/config/reader/json/values.go (about) 1 package json 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "strconv" 7 "strings" 8 "time" 9 10 simple "github.com/bitly/go-simplejson" 11 "github.com/annwntech/go-micro/v2/config/reader" 12 "github.com/annwntech/go-micro/v2/config/source" 13 ) 14 15 type jsonValues struct { 16 ch *source.ChangeSet 17 sj *simple.Json 18 } 19 20 type jsonValue struct { 21 *simple.Json 22 } 23 24 func newValues(ch *source.ChangeSet) (reader.Values, error) { 25 sj := simple.New() 26 data, _ := reader.ReplaceEnvVars(ch.Data) 27 if err := sj.UnmarshalJSON(data); err != nil { 28 sj.SetPath(nil, string(ch.Data)) 29 } 30 return &jsonValues{ch, sj}, nil 31 } 32 33 func (j *jsonValues) Get(path ...string) reader.Value { 34 return &jsonValue{j.sj.GetPath(path...)} 35 } 36 37 func (j *jsonValues) Del(path ...string) { 38 // delete the tree? 39 if len(path) == 0 { 40 j.sj = simple.New() 41 return 42 } 43 44 if len(path) == 1 { 45 j.sj.Del(path[0]) 46 return 47 } 48 49 vals := j.sj.GetPath(path[:len(path)-1]...) 50 vals.Del(path[len(path)-1]) 51 j.sj.SetPath(path[:len(path)-1], vals.Interface()) 52 return 53 } 54 55 func (j *jsonValues) Set(val interface{}, path ...string) { 56 j.sj.SetPath(path, val) 57 } 58 59 func (j *jsonValues) Bytes() []byte { 60 b, _ := j.sj.MarshalJSON() 61 return b 62 } 63 64 func (j *jsonValues) Map() map[string]interface{} { 65 m, _ := j.sj.Map() 66 return m 67 } 68 69 func (j *jsonValues) Scan(v interface{}) error { 70 b, err := j.sj.MarshalJSON() 71 if err != nil { 72 return err 73 } 74 return json.Unmarshal(b, v) 75 } 76 77 func (j *jsonValues) String() string { 78 return "json" 79 } 80 81 func (j *jsonValue) Bool(def bool) bool { 82 b, err := j.Json.Bool() 83 if err == nil { 84 return b 85 } 86 87 str, ok := j.Interface().(string) 88 if !ok { 89 return def 90 } 91 92 b, err = strconv.ParseBool(str) 93 if err != nil { 94 return def 95 } 96 97 return b 98 } 99 100 func (j *jsonValue) Int(def int) int { 101 i, err := j.Json.Int() 102 if err == nil { 103 return i 104 } 105 106 str, ok := j.Interface().(string) 107 if !ok { 108 return def 109 } 110 111 i, err = strconv.Atoi(str) 112 if err != nil { 113 return def 114 } 115 116 return i 117 } 118 119 func (j *jsonValue) String(def string) string { 120 return j.Json.MustString(def) 121 } 122 123 func (j *jsonValue) Float64(def float64) float64 { 124 f, err := j.Json.Float64() 125 if err == nil { 126 return f 127 } 128 129 str, ok := j.Interface().(string) 130 if !ok { 131 return def 132 } 133 134 f, err = strconv.ParseFloat(str, 64) 135 if err != nil { 136 return def 137 } 138 139 return f 140 } 141 142 func (j *jsonValue) Duration(def time.Duration) time.Duration { 143 v, err := j.Json.String() 144 if err != nil { 145 return def 146 } 147 148 value, err := time.ParseDuration(v) 149 if err != nil { 150 return def 151 } 152 153 return value 154 } 155 156 func (j *jsonValue) StringSlice(def []string) []string { 157 v, err := j.Json.String() 158 if err == nil { 159 sl := strings.Split(v, ",") 160 if len(sl) > 1 { 161 return sl 162 } 163 } 164 return j.Json.MustStringArray(def) 165 } 166 167 func (j *jsonValue) StringMap(def map[string]string) map[string]string { 168 m, err := j.Json.Map() 169 if err != nil { 170 return def 171 } 172 173 res := map[string]string{} 174 175 for k, v := range m { 176 res[k] = fmt.Sprintf("%v", v) 177 } 178 179 return res 180 } 181 182 func (j *jsonValue) Scan(v interface{}) error { 183 b, err := j.Json.MarshalJSON() 184 if err != nil { 185 return err 186 } 187 return json.Unmarshal(b, v) 188 } 189 190 func (j *jsonValue) Bytes() []byte { 191 b, err := j.Json.Bytes() 192 if err != nil { 193 // try return marshalled 194 b, err = j.Json.MarshalJSON() 195 if err != nil { 196 return []byte{} 197 } 198 return b 199 } 200 return b 201 }