OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. |
| 4 |
| 5 package dumper |
| 6 |
| 7 import ( |
| 8 "fmt" |
| 9 |
| 10 "github.com/luci/gae/impl/memory" |
| 11 "github.com/luci/gae/service/datastore" |
| 12 "golang.org/x/net/context" |
| 13 ) |
| 14 |
| 15 type ExampleModel struct { |
| 16 Kind string `gae:"$kind,Example"` |
| 17 ID int64 `gae:"$id"` |
| 18 Parent *datastore.Key `gae:"$parent"` |
| 19 |
| 20 Vals []string |
| 21 Number int64 |
| 22 HexNumber int64 |
| 23 } |
| 24 |
| 25 func ExampleConfig_Query() { |
| 26 c := context.Background() |
| 27 c = memory.Use(c) |
| 28 |
| 29 ds := datastore.Get(c) |
| 30 root := ds.MakeKey("Parent", 1) |
| 31 models := []*ExampleModel{ |
| 32 {ID: 1, Vals: []string{"hi", "there"}, Number: 10, HexNumber: 20
}, |
| 33 {ID: 2, Vals: []string{"other"}, Number: 11, HexNumber: 21}, |
| 34 {ID: 1, Parent: root, Vals: []string{"child", "ent"}}, |
| 35 {Kind: "Other", ID: 1, Vals: []string{"other"}, Number: 11, HexN
umber: 21}, |
| 36 } |
| 37 if err := ds.Put(models); err != nil { |
| 38 panic(err) |
| 39 } |
| 40 // indexes must be up-to-date here. |
| 41 ds.Testable().CatchupIndexes() |
| 42 |
| 43 _, err := Config{ |
| 44 PropFilters: PropFilterMap{ |
| 45 {"Example", "HexNumber"}: func(p datastore.Property) str
ing { |
| 46 return fmt.Sprintf("0x%04x", p.Value()) |
| 47 }, |
| 48 }, |
| 49 KindFilters: KindFilterMap{ |
| 50 "Other": func(key *datastore.Key, pm datastore.PropertyM
ap) string { |
| 51 return "I AM A BANANA" |
| 52 }, |
| 53 }, |
| 54 }.Query(c, nil) |
| 55 if err != nil { |
| 56 panic(err) |
| 57 } |
| 58 |
| 59 // Output: |
| 60 // dev~app::/Example,1: |
| 61 // "HexNumber": [0x0014] |
| 62 // "Number": [PTInt(10)] |
| 63 // "Vals": [ |
| 64 // PTString("hi"), |
| 65 // PTString("there") |
| 66 // ] |
| 67 // |
| 68 // dev~app::/Example,2: |
| 69 // "HexNumber": [0x0015] |
| 70 // "Number": [PTInt(11)] |
| 71 // "Vals": [PTString("other")] |
| 72 // |
| 73 // dev~app::/Other,1: |
| 74 // I AM A BANANA |
| 75 // |
| 76 // dev~app::/Parent,1/Example,1: |
| 77 // "HexNumber": [0x0000] |
| 78 // "Number": [PTInt(0)] |
| 79 // "Vals": [ |
| 80 // PTString("child"), |
| 81 // PTString("ent") |
| 82 // ] |
| 83 } |
OLD | NEW |