-
Notifications
You must be signed in to change notification settings - Fork 319
Add a package for yaml templating #1243
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") | ||
|
|
||
| go_library( | ||
| name = "go_default_library", | ||
| srcs = ["yamlutil.go"], | ||
| importpath = "github.com/pipe-cd/pipe/pkg/yamlutil", | ||
| visibility = ["//visibility:public"], | ||
| deps = [ | ||
| "@com_github_goccy_go_yaml//:go_default_library", | ||
| "@com_github_goccy_go_yaml//ast:go_default_library", | ||
| "@com_github_goccy_go_yaml//parser:go_default_library", | ||
| ], | ||
| ) | ||
|
|
||
| go_test( | ||
| name = "go_default_test", | ||
| size = "small", | ||
| srcs = ["yamlutil_test.go"], | ||
| embed = [":go_default_library"], | ||
| deps = ["@com_github_stretchr_testify//assert:go_default_library"], | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| // Copyright 2020 The PipeCD Authors. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package yamlutil | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "fmt" | ||
| "io" | ||
|
|
||
| gyaml "github.com/goccy/go-yaml" | ||
|
nakabonne marked this conversation as resolved.
Outdated
|
||
| "github.com/goccy/go-yaml/ast" | ||
| "github.com/goccy/go-yaml/parser" | ||
| ) | ||
|
|
||
| // GetValue gives back the value placed at a given path. | ||
| // | ||
| // The path requires to start with "$" which represents the root element. | ||
| // Available operators are: | ||
| // $ : the root object/element | ||
| // . : child operator | ||
| // .. : recursive descent | ||
| // [num] : object/element of array by number | ||
| // [*] : all objects/elements for array. | ||
| // | ||
| // e.g. "$.foo.bar[0].baz" | ||
| func GetValue(yml []byte, path string) (interface{}, error) { | ||
| if len(yml) == 0 { | ||
| return nil, fmt.Errorf("empty yaml given") | ||
| } | ||
| if path == "" { | ||
| return nil, fmt.Errorf("no path given") | ||
| } | ||
|
|
||
| p, err := gyaml.PathString(path) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| var value interface{} | ||
| if err := p.Read(bytes.NewReader(yml), &value); err != nil { | ||
| return nil, err | ||
| } | ||
| return value, nil | ||
| } | ||
|
|
||
| // ReplaceValue replaces the value placed at a given path with | ||
| // a given value, and then gives back the new yaml bytes. | ||
| // | ||
| // For available operators for the path, see GetValue(). | ||
| func ReplaceValue(yml []byte, path string, value string) ([]byte, error) { | ||
| if len(yml) == 0 { | ||
| return nil, fmt.Errorf("empty yaml given") | ||
| } | ||
| if path == "" { | ||
| return nil, fmt.Errorf("no path given") | ||
| } | ||
|
|
||
| p, err := gyaml.PathString(path) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| file, err := parser.ParseBytes(yml, 0) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| // Retrieve the current node placed at the specified path. | ||
| oldNode, err := p.FilterFile(file) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| newNode := &ast.StringNode{ | ||
| BaseNode: &ast.BaseNode{}, | ||
| Token: oldNode.GetToken(), | ||
| Value: value, | ||
| } | ||
|
|
||
| err = p.ReplaceWithNode(file, newNode) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| buf := new(bytes.Buffer) | ||
| _, err = io.Copy(buf, file) | ||
| return buf.Bytes(), err | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,212 @@ | ||
| // Copyright 2020 The PipeCD Authors. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package yamlutil | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestGetValue(t *testing.T) { | ||
| testcases := []struct { | ||
| name string | ||
| yml string | ||
| path string | ||
| want interface{} | ||
| wantErr bool | ||
| }{ | ||
| { | ||
| name: "empty yaml given", | ||
| yml: "", | ||
| path: "$.foo", | ||
| wantErr: true, | ||
| }, | ||
| { | ||
| name: "empty path given", | ||
| yml: "foo: bar", | ||
| path: "", | ||
| wantErr: true, | ||
| }, | ||
| { | ||
| name: "wrong path given", | ||
| yml: "foo: bar", | ||
| path: "wrong", | ||
| wantErr: true, | ||
| }, | ||
| { | ||
| name: "wrong yaml given", | ||
| yml: "::", | ||
| path: "$.foo", | ||
| wantErr: true, | ||
| }, | ||
| { | ||
| name: "lack of root element", | ||
| yml: "foo: bar", | ||
| path: "foo", | ||
| wantErr: true, | ||
| }, | ||
| { | ||
| name: "given a string path", | ||
| yml: "foo: bar", | ||
| path: "$.foo", | ||
| want: "bar", | ||
| wantErr: false, | ||
| }, | ||
| { | ||
| name: "given a bool path", | ||
| yml: "foo: true", | ||
| path: "$.foo", | ||
| want: true, | ||
| wantErr: false, | ||
| }, | ||
| { | ||
| name: "given a uint64 path", | ||
| yml: "foo: 1", | ||
| path: "$.foo", | ||
| want: uint64(1), | ||
| wantErr: false, | ||
| }, | ||
| { | ||
| name: "given a float64 path", | ||
| yml: "foo: 1.5", | ||
| path: "$.foo", | ||
| want: 1.5, | ||
| wantErr: false, | ||
| }, | ||
| { | ||
| name: "given a array path", | ||
| yml: ` | ||
| foo: | ||
| - bar: 1`, | ||
| path: "$.foo[0].bar", | ||
| want: uint64(1), | ||
| wantErr: false, | ||
| }, | ||
| { | ||
| name: "given a array path with wildcard", | ||
| yml: ` | ||
| foo: | ||
| - bar: 1 | ||
| - baz: 2`, | ||
| path: "$.foo[*]", | ||
| want: []interface{}{map[string]interface{}{"bar": uint64(1)}, map[string]interface{}{"baz": uint64(2)}}, | ||
| wantErr: false, | ||
| }, | ||
| } | ||
| for _, tc := range testcases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| got, err := GetValue([]byte(tc.yml), tc.path) | ||
| assert.Equal(t, tc.wantErr, err != nil) | ||
| assert.Equal(t, tc.want, got) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestReplaceValue(t *testing.T) { | ||
| testcases := []struct { | ||
| name string | ||
| yml string | ||
| path string | ||
| value string | ||
| want []byte | ||
| wantErr bool | ||
| }{ | ||
| { | ||
| name: "empty yaml given", | ||
| yml: "", | ||
| path: "$.foo", | ||
| value: "new-text", | ||
| wantErr: true, | ||
| }, | ||
| { | ||
| name: "empty path given", | ||
|
nakabonne marked this conversation as resolved.
|
||
| yml: "foo: bar", | ||
| path: "", | ||
| value: "new-text", | ||
| wantErr: true, | ||
| }, | ||
| { | ||
| name: "wrong path given", | ||
| yml: "foo: bar", | ||
| path: "wrong", | ||
| value: "new-text", | ||
| wantErr: true, | ||
| }, | ||
| { | ||
| name: "wrong yaml given", | ||
| yml: "::", | ||
| path: "$.foo", | ||
| value: "new-text", | ||
| wantErr: true, | ||
| }, | ||
| { | ||
| name: "empty value given", | ||
| yml: "foo: bar", | ||
| path: "$.foo", | ||
| value: "", | ||
| want: []byte("foo: "), | ||
| wantErr: false, | ||
| }, | ||
| { | ||
| name: "valid value given", | ||
| yml: "foo: bar", | ||
| path: "$.foo", | ||
| value: "new-text", | ||
| want: []byte("foo: new-text"), | ||
| wantErr: false, | ||
| }, | ||
| { | ||
| name: "array in block style", | ||
| yml: `foo: | ||
| - bar | ||
| - baz`, | ||
| path: "$.foo[0]", | ||
| value: "new-text", | ||
| want: []byte(`foo: | ||
| - new-text | ||
| - baz`), | ||
| wantErr: false, | ||
| }, | ||
| { | ||
| name: "array in flow style", | ||
| yml: `foo: [bar, baz]`, | ||
| path: "$.foo[0]", | ||
| value: "new-text", | ||
| want: []byte(`foo: [new-text, baz]`), | ||
| wantErr: false, | ||
| }, | ||
| { | ||
| name: "there is an useless blank line", | ||
| yml: ` | ||
| foo: | ||
| - bar | ||
| - baz`, | ||
| path: "$.foo[0]", | ||
| value: "new-text", | ||
| want: []byte(`foo: | ||
| - new-text | ||
| - baz`), | ||
| wantErr: false, | ||
| }, | ||
| } | ||
| for _, tc := range testcases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| got, err := ReplaceValue([]byte(tc.yml), tc.path, tc.value) | ||
| assert.Equal(t, tc.wantErr, err != nil) | ||
| assert.Equal(t, tc.want, got) | ||
| }) | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.