getDocument gets the value of the given field path, which must be a document. If create is true, it creates intermediate documents as needed.
(fp []string, create bool)
| 78 | // getDocument gets the value of the given field path, which must be a document. |
| 79 | // If create is true, it creates intermediate documents as needed. |
| 80 | func (d Document) getDocument(fp []string, create bool) (Document, error) { |
| 81 | if len(fp) == 0 { |
| 82 | return d, nil |
| 83 | } |
| 84 | x, err := d.GetField(fp[0]) |
| 85 | if err != nil { |
| 86 | if create && gcerrors.Code(err) == gcerrors.NotFound { |
| 87 | // TODO(jba): create the right type for the struct field. |
| 88 | x = map[string]any{} |
| 89 | if err := d.SetField(fp[0], x); err != nil { |
| 90 | return Document{}, err |
| 91 | } |
| 92 | } else { |
| 93 | return Document{}, err |
| 94 | } |
| 95 | } |
| 96 | d2, err := NewDocument(x) |
| 97 | if err != nil { |
| 98 | return Document{}, err |
| 99 | } |
| 100 | return d2.getDocument(fp[1:], create) |
| 101 | } |
| 102 | |
| 103 | // Get returns the value of the given field path in the document. |
| 104 | func (d Document) Get(fp []string) (any, error) { |