path_value.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. package awsutil
  2. import (
  3. "reflect"
  4. "regexp"
  5. "strconv"
  6. "strings"
  7. )
  8. var indexRe = regexp.MustCompile(`(.+)\[(-?\d+)?\]$`)
  9. // rValuesAtPath returns a slice of values found in value v. The values
  10. // in v are explored recursively so all nested values are collected.
  11. func rValuesAtPath(v interface{}, path string, create bool, caseSensitive bool) []reflect.Value {
  12. pathparts := strings.Split(path, "||")
  13. if len(pathparts) > 1 {
  14. for _, pathpart := range pathparts {
  15. vals := rValuesAtPath(v, pathpart, create, caseSensitive)
  16. if vals != nil && len(vals) > 0 {
  17. return vals
  18. }
  19. }
  20. return nil
  21. }
  22. values := []reflect.Value{reflect.Indirect(reflect.ValueOf(v))}
  23. components := strings.Split(path, ".")
  24. for len(values) > 0 && len(components) > 0 {
  25. var index *int64
  26. var indexStar bool
  27. c := strings.TrimSpace(components[0])
  28. if c == "" { // no actual component, illegal syntax
  29. return nil
  30. } else if caseSensitive && c != "*" && strings.ToLower(c[0:1]) == c[0:1] {
  31. // TODO normalize case for user
  32. return nil // don't support unexported fields
  33. }
  34. // parse this component
  35. if m := indexRe.FindStringSubmatch(c); m != nil {
  36. c = m[1]
  37. if m[2] == "" {
  38. index = nil
  39. indexStar = true
  40. } else {
  41. i, _ := strconv.ParseInt(m[2], 10, 32)
  42. index = &i
  43. indexStar = false
  44. }
  45. }
  46. nextvals := []reflect.Value{}
  47. for _, value := range values {
  48. // pull component name out of struct member
  49. if value.Kind() != reflect.Struct {
  50. continue
  51. }
  52. if c == "*" { // pull all members
  53. for i := 0; i < value.NumField(); i++ {
  54. if f := reflect.Indirect(value.Field(i)); f.IsValid() {
  55. nextvals = append(nextvals, f)
  56. }
  57. }
  58. continue
  59. }
  60. value = value.FieldByNameFunc(func(name string) bool {
  61. if c == name {
  62. return true
  63. } else if !caseSensitive && strings.ToLower(name) == strings.ToLower(c) {
  64. return true
  65. }
  66. return false
  67. })
  68. if create && value.Kind() == reflect.Ptr && value.IsNil() {
  69. value.Set(reflect.New(value.Type().Elem()))
  70. value = value.Elem()
  71. } else {
  72. value = reflect.Indirect(value)
  73. }
  74. if value.IsValid() {
  75. nextvals = append(nextvals, value)
  76. }
  77. }
  78. values = nextvals
  79. if indexStar || index != nil {
  80. nextvals = []reflect.Value{}
  81. for _, value := range values {
  82. value := reflect.Indirect(value)
  83. if value.Kind() != reflect.Slice {
  84. continue
  85. }
  86. if indexStar { // grab all indices
  87. for i := 0; i < value.Len(); i++ {
  88. idx := reflect.Indirect(value.Index(i))
  89. if idx.IsValid() {
  90. nextvals = append(nextvals, idx)
  91. }
  92. }
  93. continue
  94. }
  95. // pull out index
  96. i := int(*index)
  97. if i >= value.Len() { // check out of bounds
  98. if create {
  99. // TODO resize slice
  100. } else {
  101. continue
  102. }
  103. } else if i < 0 { // support negative indexing
  104. i = value.Len() + i
  105. }
  106. value = reflect.Indirect(value.Index(i))
  107. if value.IsValid() {
  108. nextvals = append(nextvals, value)
  109. }
  110. }
  111. values = nextvals
  112. }
  113. components = components[1:]
  114. }
  115. return values
  116. }
  117. // ValuesAtPath returns a list of objects at the lexical path inside of a structure
  118. func ValuesAtPath(i interface{}, path string) []interface{} {
  119. if rvals := rValuesAtPath(i, path, false, true); rvals != nil {
  120. vals := make([]interface{}, len(rvals))
  121. for i, rval := range rvals {
  122. vals[i] = rval.Interface()
  123. }
  124. return vals
  125. }
  126. return nil
  127. }
  128. // ValuesAtAnyPath returns a list of objects at the case-insensitive lexical
  129. // path inside of a structure
  130. func ValuesAtAnyPath(i interface{}, path string) []interface{} {
  131. if rvals := rValuesAtPath(i, path, false, false); rvals != nil {
  132. vals := make([]interface{}, len(rvals))
  133. for i, rval := range rvals {
  134. vals[i] = rval.Interface()
  135. }
  136. return vals
  137. }
  138. return nil
  139. }
  140. // SetValueAtPath sets an object at the lexical path inside of a structure
  141. func SetValueAtPath(i interface{}, path string, v interface{}) {
  142. if rvals := rValuesAtPath(i, path, true, true); rvals != nil {
  143. for _, rval := range rvals {
  144. rval.Set(reflect.ValueOf(v))
  145. }
  146. }
  147. }
  148. // SetValueAtAnyPath sets an object at the case insensitive lexical path inside
  149. // of a structure
  150. func SetValueAtAnyPath(i interface{}, path string, v interface{}) {
  151. if rvals := rValuesAtPath(i, path, true, false); rvals != nil {
  152. for _, rval := range rvals {
  153. rval.Set(reflect.ValueOf(v))
  154. }
  155. }
  156. }