gitee.com/liuxuezhan/go-micro-v1.18.0@v1.0.0/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  	"gitee.com/liuxuezhan/go-micro-v1.18.0/config/reader"
    12  	"gitee.com/liuxuezhan/go-micro-v1.18.0/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 newValue(s *simple.Json) reader.Value {
    34  	if s == nil {
    35  		s = simple.New()
    36  	}
    37  	return &jsonValue{s}
    38  }
    39  
    40  func (j *jsonValues) Get(path ...string) reader.Value {
    41  	return &jsonValue{j.sj.GetPath(path...)}
    42  }
    43  
    44  func (j *jsonValues) Del(path ...string) {
    45  	// delete the tree?
    46  	if len(path) == 0 {
    47  		j.sj = simple.New()
    48  		return
    49  	}
    50  
    51  	if len(path) == 1 {
    52  		j.sj.Del(path[0])
    53  		return
    54  	}
    55  
    56  	vals := j.sj.GetPath(path[:len(path)-1]...)
    57  	vals.Del(path[len(path)-1])
    58  	j.sj.SetPath(path[:len(path)-1], vals.Interface())
    59  	return
    60  }
    61  
    62  func (j *jsonValues) Set(val interface{}, path ...string) {
    63  	j.sj.SetPath(path, val)
    64  }
    65  
    66  func (j *jsonValues) Bytes() []byte {
    67  	b, _ := j.sj.MarshalJSON()
    68  	return b
    69  }
    70  
    71  func (j *jsonValues) Map() map[string]interface{} {
    72  	m, _ := j.sj.Map()
    73  	return m
    74  }
    75  
    76  func (j *jsonValues) Scan(v interface{}) error {
    77  	b, err := j.sj.MarshalJSON()
    78  	if err != nil {
    79  		return err
    80  	}
    81  	return json.Unmarshal(b, v)
    82  }
    83  
    84  func (j *jsonValues) String() string {
    85  	return "json"
    86  }
    87  
    88  func (j *jsonValue) Bool(def bool) bool {
    89  	b, err := j.Json.Bool()
    90  	if err == nil {
    91  		return b
    92  	}
    93  
    94  	str, ok := j.Interface().(string)
    95  	if !ok {
    96  		return def
    97  	}
    98  
    99  	b, err = strconv.ParseBool(str)
   100  	if err != nil {
   101  		return def
   102  	}
   103  
   104  	return b
   105  }
   106  
   107  func (j *jsonValue) Int(def int) int {
   108  	i, err := j.Json.Int()
   109  	if err == nil {
   110  		return i
   111  	}
   112  
   113  	str, ok := j.Interface().(string)
   114  	if !ok {
   115  		return def
   116  	}
   117  
   118  	i, err = strconv.Atoi(str)
   119  	if err != nil {
   120  		return def
   121  	}
   122  
   123  	return i
   124  }
   125  
   126  func (j *jsonValue) String(def string) string {
   127  	return j.Json.MustString(def)
   128  }
   129  
   130  func (j *jsonValue) Float64(def float64) float64 {
   131  	f, err := j.Json.Float64()
   132  	if err == nil {
   133  		return f
   134  	}
   135  
   136  	str, ok := j.Interface().(string)
   137  	if !ok {
   138  		return def
   139  	}
   140  
   141  	f, err = strconv.ParseFloat(str, 64)
   142  	if err != nil {
   143  		return def
   144  	}
   145  
   146  	return f
   147  }
   148  
   149  func (j *jsonValue) Duration(def time.Duration) time.Duration {
   150  	v, err := j.Json.String()
   151  	if err != nil {
   152  		return def
   153  	}
   154  
   155  	value, err := time.ParseDuration(v)
   156  	if err != nil {
   157  		return def
   158  	}
   159  
   160  	return value
   161  }
   162  
   163  func (j *jsonValue) StringSlice(def []string) []string {
   164  	v, err := j.Json.String()
   165  	if err == nil {
   166  		sl := strings.Split(v, ",")
   167  		if len(sl) > 1 {
   168  			return sl
   169  		}
   170  	}
   171  	return j.Json.MustStringArray(def)
   172  }
   173  
   174  func (j *jsonValue) StringMap(def map[string]string) map[string]string {
   175  	m, err := j.Json.Map()
   176  	if err != nil {
   177  		return def
   178  	}
   179  
   180  	res := map[string]string{}
   181  
   182  	for k, v := range m {
   183  		res[k] = fmt.Sprintf("%v", v)
   184  	}
   185  
   186  	return res
   187  }
   188  
   189  func (j *jsonValue) Scan(v interface{}) error {
   190  	b, err := j.Json.MarshalJSON()
   191  	if err != nil {
   192  		return err
   193  	}
   194  	return json.Unmarshal(b, v)
   195  }
   196  
   197  func (j *jsonValue) Bytes() []byte {
   198  	b, err := j.Json.Bytes()
   199  	if err != nil {
   200  		// try return marshalled
   201  		b, err = j.Json.MarshalJSON()
   202  		if err != nil {
   203  			return []byte{}
   204  		}
   205  		return b
   206  	}
   207  	return b
   208  }