Skip to content
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

Support WithComment option #238

Merged
merged 3 commits into from
Jul 20, 2021
Merged

Support WithComment option #238

merged 3 commits into from
Jul 20, 2021

Conversation

goccy
Copy link
Owner

@goccy goccy commented Jul 20, 2021

Add option for encoding with comment .

Please see the following test case .

go-yaml/yaml_test.go

Lines 386 to 468 in 3c954f0

func Test_WithCommentOption(t *testing.T) {
v := struct {
Foo string `yaml:"foo"`
Bar map[string]interface{} `yaml:"bar"`
Baz struct {
X int `yaml:"x"`
} `yaml:"baz"`
}{
Foo: "aaa",
Bar: map[string]interface{}{"bbb": "ccc"},
Baz: struct {
X int `yaml:"x"`
}{X: 10},
}
t.Run("line comment", func(t *testing.T) {
b, err := yaml.MarshalWithOptions(v, yaml.WithComment(
yaml.CommentMap{
"$.foo": yaml.LineComment("foo comment"),
"$.bar": yaml.LineComment("bar comment"),
"$.bar.bbb": yaml.LineComment("bbb comment"),
"$.baz.x": yaml.LineComment("x comment"),
},
))
if err != nil {
t.Fatal(err)
}
expected := `
foo: aaa #foo comment
bar: #bar comment
bbb: ccc #bbb comment
baz:
x: 10 #x comment
`
actual := "\n" + string(b)
if expected != actual {
t.Fatalf("expected:%s but got %s", expected, actual)
}
})
t.Run("head comment", func(t *testing.T) {
b, err := yaml.MarshalWithOptions(v, yaml.WithComment(
yaml.CommentMap{
"$.foo": yaml.HeadComment(
"foo comment",
"foo comment2",
),
"$.bar": yaml.HeadComment(
"bar comment",
"bar comment2",
),
"$.bar.bbb": yaml.HeadComment(
"bbb comment",
"bbb comment2",
),
"$.baz.x": yaml.HeadComment(
"x comment",
"x comment2",
),
},
))
if err != nil {
t.Fatal(err)
}
expected := `
#foo comment
#foo comment2
foo: aaa
#bar comment
#bar comment2
bar:
#bbb comment
#bbb comment2
bbb: ccc
baz:
#x comment
#x comment2
x: 10
`
actual := "\n" + string(b)
if expected != actual {
t.Fatalf("expected:%s but got %s", expected, actual)
}
})
}

yaml.WithComment is a option for encoding with comment .
The position where you want to add a comment is represented by YAMLPath, and it is the key of yaml.CommentMap .
Also, you can select Head comment or Line comment as the comment type.

@codecov-commenter
Copy link

codecov-commenter commented Jul 20, 2021

Codecov Report

Merging #238 (225a4d7) into master (cc0aeb9) will decrease coverage by 0.47%.
The diff coverage is 52.45%.

@@            Coverage Diff             @@
##           master     #238      +/-   ##
==========================================
- Coverage   78.84%   78.37%   -0.48%     
==========================================
  Files          12       12              
  Lines        3356     3417      +61     
==========================================
+ Hits         2646     2678      +32     
- Misses        482      505      +23     
- Partials      228      234       +6     

@nakabonne
Copy link
Contributor

I know it's quite tedious to extract comments from the AST so one vote for this option. I have no opportunity to add comments when encoding for now though.

@goccy goccy merged commit 2742b89 into master Jul 20, 2021
@goccy goccy deleted the feature/with-comment branch July 20, 2021 10:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants