github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/service/config/value.go (about)

     1  // Licensed under the Apache License, Version 2.0 (the "License");
     2  // you may not use this file except in compliance with the License.
     3  // You may obtain a copy of the License at
     4  //
     5  //     https://www.apache.org/licenses/LICENSE-2.0
     6  //
     7  // Unless required by applicable law or agreed to in writing, software
     8  // distributed under the License is distributed on an "AS IS" BASIS,
     9  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    10  // See the License for the specific language governing permissions and
    11  // limitations under the License.
    12  //
    13  // Original source: github.com/micro/go-micro/v3/config/value.go
    14  
    15  package config
    16  
    17  import (
    18  	"encoding/json"
    19  	"fmt"
    20  	"strconv"
    21  	"strings"
    22  	"time"
    23  
    24  	simple "github.com/bitly/go-simplejson"
    25  )
    26  
    27  type JSONValues struct {
    28  	values []byte
    29  	sj     *simple.Json
    30  }
    31  
    32  type JSONValue struct {
    33  	*simple.Json
    34  }
    35  
    36  func NewJSONValues(data []byte) *JSONValues {
    37  	sj := simple.New()
    38  
    39  	if err := sj.UnmarshalJSON(data); err != nil {
    40  		sj.SetPath(nil, string(data))
    41  	}
    42  	return &JSONValues{data, sj}
    43  }
    44  
    45  func NewJSONValue(data []byte) *JSONValue {
    46  	sj := simple.New()
    47  
    48  	if err := sj.UnmarshalJSON(data); err != nil {
    49  		sj.SetPath(nil, string(data))
    50  	}
    51  	return &JSONValue{sj}
    52  }
    53  
    54  func (j *JSONValues) Get(path string, options ...Option) Value {
    55  	paths := strings.Split(path, ".")
    56  	return &JSONValue{j.sj.GetPath(paths...)}
    57  }
    58  
    59  func (j *JSONValues) Delete(path string, options ...Option) {
    60  	paths := strings.Split(path, ".")
    61  	// delete the tree?
    62  	if len(paths) == 0 {
    63  		j.sj = simple.New()
    64  		return
    65  	}
    66  
    67  	if len(paths) == 1 {
    68  		j.sj.Del(paths[0])
    69  		return
    70  	}
    71  
    72  	vals := j.sj.GetPath(paths[:len(paths)-1]...)
    73  	vals.Del(paths[len(paths)-1])
    74  	j.sj.SetPath(paths[:len(paths)-1], vals.Interface())
    75  	return
    76  }
    77  
    78  func (j *JSONValues) Set(path string, val interface{}, options ...Option) {
    79  	paths := strings.Split(path, ".")
    80  	j.sj.SetPath(paths, val)
    81  }
    82  
    83  func (j *JSONValues) Bytes() []byte {
    84  	b, _ := j.sj.MarshalJSON()
    85  	return b
    86  }
    87  
    88  func (j *JSONValues) Map() map[string]interface{} {
    89  	m, _ := j.sj.Map()
    90  	return m
    91  }
    92  
    93  func (j *JSONValues) Scan(v interface{}) error {
    94  	b, err := j.sj.MarshalJSON()
    95  	if err != nil {
    96  		return err
    97  	}
    98  	return json.Unmarshal(b, v)
    99  }
   100  
   101  func (j *JSONValues) String() string {
   102  	return "json"
   103  }
   104  
   105  func (j *JSONValue) Bool(def bool) bool {
   106  	b, err := j.Json.Bool()
   107  	if err == nil {
   108  		return b
   109  	}
   110  
   111  	str, ok := j.Interface().(string)
   112  	if !ok {
   113  		return def
   114  	}
   115  
   116  	b, err = strconv.ParseBool(str)
   117  	if err != nil {
   118  		return def
   119  	}
   120  
   121  	return b
   122  }
   123  
   124  func (j *JSONValue) Int(def int) int {
   125  	i, err := j.Json.Int()
   126  	if err == nil {
   127  		return i
   128  	}
   129  
   130  	str, ok := j.Interface().(string)
   131  	if !ok {
   132  		return def
   133  	}
   134  
   135  	i, err = strconv.Atoi(str)
   136  	if err != nil {
   137  		return def
   138  	}
   139  
   140  	return i
   141  }
   142  
   143  func (j *JSONValue) String(def string) string {
   144  	return j.Json.MustString(def)
   145  }
   146  
   147  func (j *JSONValue) Float64(def float64) float64 {
   148  	f, err := j.Json.Float64()
   149  	if err == nil {
   150  		return f
   151  	}
   152  
   153  	str, ok := j.Interface().(string)
   154  	if !ok {
   155  		return def
   156  	}
   157  
   158  	f, err = strconv.ParseFloat(str, 64)
   159  	if err != nil {
   160  		return def
   161  	}
   162  
   163  	return f
   164  }
   165  
   166  func (j *JSONValue) Duration(def time.Duration) time.Duration {
   167  	v, err := j.Json.String()
   168  	if err != nil {
   169  		return def
   170  	}
   171  
   172  	value, err := time.ParseDuration(v)
   173  	if err != nil {
   174  		return def
   175  	}
   176  
   177  	return value
   178  }
   179  
   180  func (j *JSONValue) StringSlice(def []string) []string {
   181  	v, err := j.Json.String()
   182  	if err == nil {
   183  		sl := strings.Split(v, ",")
   184  		if len(sl) > 1 {
   185  			return sl
   186  		}
   187  	}
   188  	return j.Json.MustStringArray(def)
   189  }
   190  
   191  func (j *JSONValue) Exists() bool {
   192  	return false
   193  }
   194  
   195  func (j *JSONValue) StringMap(def map[string]string) map[string]string {
   196  	m, err := j.Json.Map()
   197  	if err != nil {
   198  		return def
   199  	}
   200  
   201  	res := map[string]string{}
   202  
   203  	for k, v := range m {
   204  		res[k] = fmt.Sprintf("%v", v)
   205  	}
   206  
   207  	return res
   208  }
   209  
   210  func (j *JSONValue) Scan(v interface{}) error {
   211  	b, err := j.Json.MarshalJSON()
   212  	if err != nil {
   213  		return err
   214  	}
   215  	return json.Unmarshal(b, v)
   216  }
   217  
   218  func (j *JSONValue) Bytes() []byte {
   219  	b, err := j.Json.Bytes()
   220  	if err != nil {
   221  		// try return marshalled
   222  		b, err = j.Json.MarshalJSON()
   223  		if err != nil {
   224  			return []byte{}
   225  		}
   226  		return b
   227  	}
   228  	return b
   229  }