1
0

values.go 3.2 KB

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