utils.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package jsonutils
  15. import (
  16. "fmt"
  17. "time"
  18. "yunion.io/x/pkg/errors"
  19. )
  20. func NewStringArray(arr []string) *JSONArray {
  21. ret := NewArray()
  22. for _, a := range arr {
  23. ret.Add(NewString(a))
  24. }
  25. return ret
  26. }
  27. func (this *JSONArray) GetStringArray() []string {
  28. ret := make([]string, len(this.data))
  29. for i, obj := range this.data {
  30. s, e := obj.GetString()
  31. if e == nil {
  32. ret[i] = s
  33. }
  34. }
  35. return ret
  36. }
  37. func JSONArray2StringArray(arr []JSONObject) []string {
  38. ret := make([]string, len(arr))
  39. for i, o := range arr {
  40. s, e := o.GetString()
  41. if e == nil {
  42. ret[i] = s
  43. }
  44. }
  45. return ret
  46. }
  47. func GetStringArray(o JSONObject, key ...string) ([]string, error) {
  48. arr, err := o.GetArray(key...)
  49. if err != nil {
  50. return nil, err
  51. }
  52. return JSONArray2StringArray(arr), nil
  53. }
  54. func NewTimeString(tm time.Time) *JSONString {
  55. return NewString(tm.UTC().Format("2006-01-02T15:04:05Z"))
  56. }
  57. func GetQueryStringArray(query JSONObject, key string) []string {
  58. if query == nil {
  59. return nil
  60. }
  61. var arr []string
  62. searchObj, _ := query.Get(key)
  63. if searchObj != nil {
  64. switch searchObj.(type) {
  65. case *JSONArray:
  66. searchArr := searchObj.(*JSONArray)
  67. arr = searchArr.GetStringArray()
  68. case *JSONString:
  69. searchText, _ := searchObj.(*JSONString).GetString()
  70. arr = []string{searchText}
  71. case *JSONDict:
  72. arr = make([]string, 0)
  73. idx := 0
  74. for {
  75. searchText, err := searchObj.GetString(fmt.Sprintf("%d", idx))
  76. if err != nil {
  77. break
  78. }
  79. arr = append(arr, searchText)
  80. idx += 1
  81. }
  82. }
  83. }
  84. return arr
  85. }
  86. func CheckRequiredFields(data JSONObject, fields []string) error {
  87. jsonMap, err := data.GetMap()
  88. if err != nil {
  89. return errors.Wrap(err, "data.GetMap") //fmt.Errorf("fail to convert input to map")
  90. }
  91. for _, f := range fields {
  92. jsonVal, ok := jsonMap[f]
  93. if !ok {
  94. return errors.Wrap(ErrMissingInputField, f)
  95. }
  96. if jsonVal == JSONNull {
  97. return errors.Wrap(ErrNilInputField, f)
  98. }
  99. }
  100. return nil
  101. }
  102. func GetAnyString(json JSONObject, keys []string) string {
  103. val, _ := GetAnyString2(json, keys)
  104. return val
  105. }
  106. func GetAnyString2(json JSONObject, keys []string) (string, string) {
  107. if json == nil {
  108. return "", ""
  109. }
  110. for _, key := range keys {
  111. val, _ := json.GetString(key)
  112. if len(val) > 0 {
  113. return val, key
  114. }
  115. }
  116. return "", ""
  117. }
  118. func GetArrayOfPrefix(json JSONObject, prefix string) []JSONObject {
  119. if json == nil {
  120. return nil
  121. }
  122. retArray := make([]JSONObject, 0)
  123. idx := 0
  124. for {
  125. obj, _ := json.Get(fmt.Sprintf("%s.%d", prefix, idx))
  126. if obj == nil || obj == JSONNull {
  127. break
  128. }
  129. retArray = append(retArray, obj)
  130. idx += 1
  131. }
  132. return retArray
  133. }