reflectValue populates the values parameter from the struct fields in val. Embedded structs are followed recursively (using the rules defined in the Values function documentation) breadth-first.
(values url.Values, val reflect.Value, scope string)
| 149 | // Embedded structs are followed recursively (using the rules defined in the |
| 150 | // Values function documentation) breadth-first. |
| 151 | func reflectValue(values url.Values, val reflect.Value, scope string) error { |
| 152 | var embedded []reflect.Value |
| 153 | |
| 154 | typ := val.Type() |
| 155 | for i := 0; i < typ.NumField(); i++ { |
| 156 | sf := typ.Field(i) |
| 157 | if sf.PkgPath != "" && !sf.Anonymous { // unexported |
| 158 | continue |
| 159 | } |
| 160 | |
| 161 | sv := val.Field(i) |
| 162 | tag := sf.Tag.Get("url") |
| 163 | if tag == "-" { |
| 164 | continue |
| 165 | } |
| 166 | name, opts := parseTag(tag) |
| 167 | |
| 168 | if name == "" { |
| 169 | if sf.Anonymous { |
| 170 | v := reflect.Indirect(sv) |
| 171 | if v.IsValid() && v.Kind() == reflect.Struct { |
| 172 | // save embedded struct for later processing |
| 173 | embedded = append(embedded, v) |
| 174 | continue |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | name = sf.Name |
| 179 | } |
| 180 | |
| 181 | if scope != "" { |
| 182 | name = scope + "[" + name + "]" |
| 183 | } |
| 184 | |
| 185 | if opts.Contains("omitempty") && isEmptyValue(sv) { |
| 186 | continue |
| 187 | } |
| 188 | |
| 189 | if sv.Type().Implements(encoderType) { |
| 190 | // if sv is a nil pointer and the custom encoder is defined on a non-pointer |
| 191 | // method receiver, set sv to the zero value of the underlying type |
| 192 | if !reflect.Indirect(sv).IsValid() && sv.Type().Elem().Implements(encoderType) { |
| 193 | sv = reflect.New(sv.Type().Elem()) |
| 194 | } |
| 195 | |
| 196 | m := sv.Interface().(Encoder) |
| 197 | if err := m.EncodeValues(name, &values); err != nil { |
| 198 | return err |
| 199 | } |
| 200 | continue |
| 201 | } |
| 202 | |
| 203 | // recursively dereference pointers. break on nil pointers |
| 204 | for sv.Kind() == reflect.Ptr { |
| 205 | if sv.IsNil() { |
| 206 | break |
| 207 | } |
| 208 | sv = sv.Elem() |
no test coverage detected
searching dependent graphs…