read: retrieve entire subdocument

This commit is contained in:
therealplato 2018-05-02 20:41:20 +12:00
parent fbeeaba875
commit abbd346659
2 changed files with 10 additions and 2 deletions

2
yq.go
View File

@ -181,7 +181,7 @@ func readProperty(cmd *cobra.Command, args []string) error {
return nil
}
var regexpSubdocPath = regexp.MustCompile(`^@(\d+)\.(.*)$`)
var regexpSubdocPath = regexp.MustCompile(`^@(\d+)(?:\.)?(.*)?$`)
func read(args []string) (interface{}, error) {
var (

View File

@ -43,7 +43,15 @@ func TestReadMultipleDocuments(t *testing.T) {
result, _ := read([]string{"examples/multidocument.yaml", "b.c"})
assertResult(t, 1, result)
})
t.Run("multiple documents with leading @n reads from document n", func(t *testing.T) {
t.Run("multiple documents with @n path reads document n", func(t *testing.T) {
result, _ := read([]string{"examples/multidocument.yaml", "@0"})
actual, err := yamlToString(result)
assertNilErr(t, err)
assertResult(t, `a: Document One
b:
c: 1`, actual)
})
t.Run("multiple documents with leading @n path reads path from document n", func(t *testing.T) {
result, _ := read([]string{"examples/multidocument.yaml", "@0.b.c"})
assertResult(t, 1, result)