Repository: goccy/go-yaml
Pull request #604 (2024-12-22 11:01:16)

Fix encoding with map key contains colon character
fix https://github.com/goccy/go-yaml/issues/174

codecov-commenter: :warning: Please install the !['codecov app svg image'](https://github.com/codecov/engineering-team/assets/152432831/e90313f4-9d3a-4b63-8b54-cfe14e7ec20d) to ensure uploads and comments are reliably processed by Codecov.

## [Codecov](https://app.codecov.io/gh/goccy/go-yaml/pull/604?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Masaaki+Goshima) Report
Attention: Patch coverage is `71.42857%` with `2 lines` in your changes missing coverage. Please review.
> Project coverage is 77.14%. Comparing base [(`e2e4400`)](https://app.codecov.io/gh/goccy/go-yaml/commit/e2e4400472676ebba2873a71a4094a15c4636479?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Masaaki+Goshima) to head [(`5f66890`)](https://app.codecov.io/gh/goccy/go-yaml/commit/5f66890e495f3a906f1e6f6eefd611a7ed3944a4?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Masaaki+Goshima).

: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     #604      +/-   ##
==========================================
- Coverage   77.15%   77.14%   -0.01%     
==========================================
  Files          22       22              
  Lines        7879     7886       +7     
==========================================
+ Hits         6079     6084       +5     
- Misses       1381     1382       +1     
- Partials      419      420       +1     
```

</details>

Patch:
diff --git a/encode.go b/encode.go
index 85080ff..265d044 100644
--- a/encode.go
+++ b/encode.go
@@ -538,6 +538,17 @@ func (e *Encoder) isNeedQuoted(v string) bool {
 	if e.isFlowStyle && strings.ContainsAny(v, `]},'"`) {
 		return true
 	}
+	if e.isFlowStyle {
+		for i := 0; i < len(v); i++ {
+			if v[i] != ':' {
+				continue
+			}
+			if i+1 < len(v) && v[i+1] == '/' {
+				continue
+			}
+			return true
+		}
+	}
 	if token.IsNeedQuoted(v) {
 		return true
 	}


Test patch:
diff --git a/encode_test.go b/encode_test.go
index b032316..bcf2f3f 100644
--- a/encode_test.go
+++ b/encode_test.go
@@ -8,6 +8,7 @@ import (
 	"math"
 	"reflect"
 	"strconv"
+	"strings"
 	"testing"
 	"time"
 
@@ -1654,3 +1655,23 @@ b:
 		t.Fatalf("failed to encode. expected %s but got %s", expected, got)
 	}
 }
+
+type Issue174 struct {
+	K string
+	V []int
+}
+
+func (v Issue174) MarshalYAML() ([]byte, error) {
+	return yaml.MarshalWithOptions(map[string][]int{v.K: v.V}, yaml.Flow(true))
+}
+
+func TestIssue174(t *testing.T) {
+	b, err := yaml.Marshal(Issue174{"00:00:00-23:59:59", []int{1, 2, 3}})
+	if err != nil {
+		t.Fatal(err)
+	}
+	got := strings.TrimSuffix(string(b), "\n")
+	if got != `{"00:00:00-23:59:59": [1, 2, 3]}` {
+		t.Fatalf("failed to encode: %q", got)
+	}
+}