/
field.go
45 lines (31 loc) · 1.58 KB
/
field.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package faunadb
// Field is a field extractor for FaunaDB values.
type Field struct{ path path }
// FieldValue describes an extracted field value.
type FieldValue interface {
GetValue() (Value, error) // GetValue returns the extracted FaunaDB value.
Get(i interface{}) error // Get decodes a FaunaDB value to a native Go type.
}
// ObjKey creates a field extractor for a JSON object based on the provided keys.
func ObjKey(keys ...string) Field { return Field{pathFromKeys(keys...)} }
// ArrIndex creates a field extractor for a JSON array based on the provided indexes.
func ArrIndex(indexes ...int) Field { return Field{pathFromIndexes(indexes...)} }
// At creates a new field extractor based on the provided path.
func (f Field) At(other Field) Field { return Field{f.path.subPath(other.path)} }
// AtKey creates a new field extractor based on the provided key.
func (f Field) AtKey(keys ...string) Field { return f.At(ObjKey(keys...)) }
// AtIndex creates a new field extractor based on the provided index.
func (f Field) AtIndex(indexes ...int) Field { return f.At(ArrIndex(indexes...)) }
func (f *Field) get(value Value) FieldValue {
value, err := f.path.get(value)
if err != nil {
return invalidField{err}
}
return validField{value}
}
type validField struct{ value Value }
func (v validField) GetValue() (Value, error) { return v.value, nil }
func (v validField) Get(i interface{}) error { return v.value.Get(i) }
type invalidField struct{ err error }
func (v invalidField) GetValue() (Value, error) { return nil, v.err }
func (v invalidField) Get(i interface{}) error { return v.err }