| 2298 | } |
| 2299 | |
| 2300 | func testExampleInDoc(t *testing.T, _ Harness, coll *docstore.Collection) { |
| 2301 | t.Helper() |
| 2302 | |
| 2303 | type Name struct { |
| 2304 | First, Last string |
| 2305 | } |
| 2306 | type Book struct { |
| 2307 | Title string `docstore:"name"` |
| 2308 | Author Name `docstore:"author"` |
| 2309 | PublicationYears []int `docstore:"pub_years,omitempty"` |
| 2310 | NumPublications int `docstore:"-"` |
| 2311 | } |
| 2312 | |
| 2313 | must := func(err error) { |
| 2314 | t.Helper() |
| 2315 | if err != nil { |
| 2316 | t.Fatal(err) |
| 2317 | } |
| 2318 | } |
| 2319 | checkFieldEqual := func(got, want any, field string) { |
| 2320 | t.Helper() |
| 2321 | fvg, err := MustDocument(got).GetField(field) |
| 2322 | must(err) |
| 2323 | fvw, err := MustDocument(want).GetField(field) |
| 2324 | must(err) |
| 2325 | if !cmp.Equal(fvg, fvw) { |
| 2326 | t.Errorf("%s: got %v want %v", field, fvg, fvw) |
| 2327 | } |
| 2328 | } |
| 2329 | |
| 2330 | doc1 := &Book{ |
| 2331 | Title: "The Master and Margarita", |
| 2332 | Author: Name{ |
| 2333 | First: "Mikhail", |
| 2334 | Last: "Bulgakov", |
| 2335 | }, |
| 2336 | PublicationYears: []int{1967, 1973}, |
| 2337 | NumPublications: 2, |
| 2338 | } |
| 2339 | |
| 2340 | doc2 := map[string]any{ |
| 2341 | KeyField: "The Heart of a Dog", |
| 2342 | "author": map[string]any{ |
| 2343 | "First": "Mikhail", |
| 2344 | "Last": "Bulgakov", |
| 2345 | }, |
| 2346 | "pub_years": []int{1968, 1987}, |
| 2347 | } |
| 2348 | |
| 2349 | ctx := context.Background() |
| 2350 | must(coll.Actions().Create(doc1).Put(doc2).Do(ctx)) |
| 2351 | got1 := &Book{Title: doc1.Title} |
| 2352 | got2 := &Book{Title: doc2[KeyField].(string)} |
| 2353 | must(coll.Actions().Get(got1).Get(got2).Do(ctx)) |
| 2354 | |
| 2355 | if got1.NumPublications != 0 { |
| 2356 | t.Errorf("docstore:\"-\" tagged field isn't ignored") |
| 2357 | } |