Skip to content

Commit

Permalink
Add Document for YAMLPath
Browse files Browse the repository at this point in the history
  • Loading branch information
goccy committed Jun 4, 2020
1 parent 5387ce3 commit 6dddb26
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ As of this writing, there already exists a defacto standard library for YAML pro
- Support `Scanner` or `Lexer` or `Parser` as public API
- Support `Anchor` and `Alias` to Marshaler
- Allow referencing elements declared in another file via anchors
- Extract value or AST by YAMLPath ( YAMLPath is like a JSONPath )

# Synopsis

Expand Down Expand Up @@ -272,6 +273,32 @@ control turning on/off these features

<img src="https://user-images.githubusercontent.com/209884/67358124-587f0980-f59a-11e9-96fc-7205aab77695.png"></img>

# 5. Use YAMLPath

```go
yml := `
store:
book:
- author: john
price: 10
- author: ken
price: 12
bicycle:
color: red
price: 19.95
`
path, err := yaml.PathString("$.store.book[*].author")
if err != nil {
...
}
var authors []string
if err := path.Read(strings.NewReader(yml), &authors); err != nil {
...
}
fmt.Println(authors)
// [john ken]
```

# Installation

```
Expand Down
27 changes: 27 additions & 0 deletions path_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package yaml_test

import (
"fmt"
"log"
"reflect"
"strings"
"testing"
Expand Down Expand Up @@ -111,3 +113,28 @@ store:
}
})
}

func Example_YAMLPath() {
yml := `
store:
book:
- author: john
price: 10
- author: ken
price: 12
bicycle:
color: red
price: 19.95
`
path, err := yaml.PathString("$.store.book[*].author")
if err != nil {
log.Fatal(err)
}
var authors []string
if err := path.Read(strings.NewReader(yml), &authors); err != nil {
log.Fatal(err)
}
fmt.Println(authors)
// OUTPUT:
// [john ken]
}

0 comments on commit 6dddb26

Please sign in to comment.