evalField evaluates field of struct or key of map.
(input []reflect.Value, node *FieldNode)
| 337 | |
| 338 | // evalField evaluates field of struct or key of map. |
| 339 | func (j *JSONPath) evalField(input []reflect.Value, node *FieldNode) ([]reflect.Value, error) { |
| 340 | results := []reflect.Value{} |
| 341 | // If there's no input, there's no output |
| 342 | if len(input) == 0 { |
| 343 | return results, nil |
| 344 | } |
| 345 | for _, value := range input { |
| 346 | var result reflect.Value |
| 347 | value, isNil := template.Indirect(value) |
| 348 | if isNil { |
| 349 | continue |
| 350 | } |
| 351 | |
| 352 | if value.Kind() == reflect.Struct { |
| 353 | var err error |
| 354 | if result, err = j.findFieldInValue(&value, node); err != nil { |
| 355 | return nil, err |
| 356 | } |
| 357 | } else if value.Kind() == reflect.Map { |
| 358 | mapKeyType := value.Type().Key() |
| 359 | nodeValue := reflect.ValueOf(node.Value) |
| 360 | // node value type must be convertible to map key type |
| 361 | if !nodeValue.Type().ConvertibleTo(mapKeyType) { |
| 362 | return results, fmt.Errorf("%s is not convertible to %s", nodeValue, mapKeyType) |
| 363 | } |
| 364 | result = value.MapIndex(nodeValue.Convert(mapKeyType)) |
| 365 | } |
| 366 | if result.IsValid() { |
| 367 | results = append(results, result) |
| 368 | } |
| 369 | } |
| 370 | if len(results) == 0 { |
| 371 | if j.allowMissingKeys { |
| 372 | return results, nil |
| 373 | } |
| 374 | return results, fmt.Errorf("%s is not found", node.Value) |
| 375 | } |
| 376 | return results, nil |
| 377 | } |
| 378 | |
| 379 | // evalWildcard extracts all contents of the given value |
| 380 | func (j *JSONPath) evalWildcard(input []reflect.Value, node *WildcardNode) ([]reflect.Value, error) { |
no test coverage detected