Repository: goccy/go-yaml
Pull request #421 (2024-01-26 07:30:35)

fix: remove any trailing empty lines if the block scalar has strip indicator
## What

SSIA

## Why

> Stripping is specified by the “-” chomping indicator. In this case, the final [line break](https://yaml.org/spec/1.2.2/#line-break-characters) and any trailing [empty lines](https://yaml.org/spec/1.2.2/#empty-lines) are excluded from the [scalar’s content](https://yaml.org/spec/1.2.2/#scalar).

https://yaml.org/spec/1.2.2/#8112-block-chomping-indicator

Before submitting your PR, please confirm the following.

- [x] Describe the purpose for which you created this PR.  
- [x] Create test code that corresponds to the modification

codecov-commenter: ## [Codecov](https://app.codecov.io/gh/goccy/go-yaml/pull/421?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Masaaki+Goshima) Report
> Merging [#421](https://app.codecov.io/gh/goccy/go-yaml/pull/421?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Masaaki+Goshima) (4618994) into [master](https://app.codecov.io/gh/goccy/go-yaml/commit/42fb764e96842b555177d841eb3a98cf09e36f7a?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Masaaki+Goshima) (42fb764) will **increase** coverage by `0.41%`.
> Report is 14 commits behind head on master.
> The diff coverage is `n/a`.

:exclamation: Your organization needs to install the [Codecov GitHub app](https://github.com/apps/codecov/installations/select_target) to enable full functionality.

<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff             @@
##           master     #421      +/-   ##
==========================================
+ Coverage   75.61%   76.02%   +0.41%     
==========================================
  Files          13       13              
  Lines        4650     4692      +42     
==========================================
+ Hits         3516     3567      +51     
+ Misses        874      866       -8     
+ Partials      260      259       -1     
```

</details>
goccy: LGTM !! Thank you for your great PR !

Patch:
diff --git a/scanner/context.go b/scanner/context.go
index 09d0a2d..3aaec56 100644
--- a/scanner/context.go
+++ b/scanner/context.go
@@ -196,9 +196,16 @@ func (c *Context) existsBuffer() bool {
 
 func (c *Context) bufferedSrc() []rune {
 	src := c.buf[:c.notSpaceCharPos]
-	if len(src) > 0 && src[len(src)-1] == '\n' && c.isDocument() && c.literalOpt == "-" {
-		// remove end '\n' character
-		src = src[:len(src)-1]
+	if c.isDocument() && c.literalOpt == "-" {
+		// remove end '\n' character and trailing empty lines
+		// https://yaml.org/spec/1.2.2/#8112-block-chomping-indicator
+		for {
+			if len(src) > 0 && src[len(src)-1] == '\n' {
+				src = src[:len(src)-1]
+				continue
+			}
+			break
+		}
 	}
 	return src
 }


Test patch:
diff --git a/decode_test.go b/decode_test.go
index 2373804..cabfd33 100644
--- a/decode_test.go
+++ b/decode_test.go
@@ -860,6 +860,14 @@ func TestDecoder(t *testing.T) {
 				},
 			},
 		},
+		{
+			"v:\n- A\n- |-\n  B\n  C\n\n\n",
+			map[string][]string{
+				"v": {
+					"A", "B\nC",
+				},
+			},
+		},
 		{
 			"v:\n- A\n- >-\n  B\n  C\n",
 			map[string][]string{
@@ -868,6 +876,14 @@ func TestDecoder(t *testing.T) {
 				},
 			},
 		},
+		{
+			"v:\n- A\n- >-\n  B\n  C\n\n\n",
+			map[string][]string{
+				"v": {
+					"A", "B C",
+				},
+			},
+		},
 		{
 			"a: b\nc: d\n",
 			struct {