field.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package apijson
  2. import "reflect"
  3. type status uint8
  4. const (
  5. missing status = iota
  6. null
  7. invalid
  8. valid
  9. )
  10. type Field struct {
  11. raw string
  12. status status
  13. }
  14. // Returns true if the field is explicitly `null` _or_ if it is not present at all (ie, missing).
  15. // To check if the field's key is present in the JSON with an explicit null value,
  16. // you must check `f.IsNull() && !f.IsMissing()`.
  17. func (j Field) IsNull() bool { return j.status <= null }
  18. func (j Field) IsMissing() bool { return j.status == missing }
  19. func (j Field) IsInvalid() bool { return j.status == invalid }
  20. func (j Field) Raw() string { return j.raw }
  21. func getSubField(root reflect.Value, index []int, name string) reflect.Value {
  22. strct := root.FieldByIndex(index[:len(index)-1])
  23. if !strct.IsValid() {
  24. panic("couldn't find encapsulating struct for field " + name)
  25. }
  26. meta := strct.FieldByName("JSON")
  27. if !meta.IsValid() {
  28. return reflect.Value{}
  29. }
  30. field := meta.FieldByName(name)
  31. if !field.IsValid() {
  32. return reflect.Value{}
  33. }
  34. return field
  35. }