Skip to content

Commit

Permalink
[DOCS] Document List.Get and the new deep query
Browse files Browse the repository at this point in the history
  • Loading branch information
fahhem committed Feb 18, 2015
1 parent 40a41f9 commit 57c0195
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions docs/structs.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,52 @@ can be referenced by ID or name. The `1` is a string because JSON only allows
keys to be strings.


## Using Lists

Given a List of something, you can use it just like a normal list.

```capnp
struct Person {
...
addresses @2 :List(Text);
}
```
A `List` of `Text` acts exactly like a list of (unicode) strings.

```python
p = Person()
p.addresses.append('address #1')
json.dumps(p) == '{"addresses": ["address #1"]}'
```

The cool part of `List`s come in when it's a `List` of structs.

```capnp
struct USAddress {
lines :group {
line1 @0 :Text;
line2 @1 :Text;
}
state @2 :Text;
...
}
struct Person {
addresses @2 :List(USAddress);
}
```

Now, we can do something interesting by getting the first element in the list
that matches our query.

```python
p = Person({'addresses': [
{'lines': {'line1': '1 Main St'}, 'state': 'NY'},
{'state': 'CA'},
]})
# Returns the second address.
p.addresses.Get(state=CA)
# This returns the first address, not just the line or lines.
p.addresses.Get(lines__line1='1 Main St').state == 'NY'
```

0 comments on commit 57c0195

Please sign in to comment.