From 0e2a1283bbc18949cacee18d9789e6393c5d0ad0 Mon Sep 17 00:00:00 2001
From: Nicolai Dagestad
Date: Sat, 8 Jan 2022 18:22:47 +0100
Subject: [PATCH 1/7] Return an empty array from ValueWithShadows if there are
none
---
key.go | 3 +++
1 file changed, 3 insertions(+)
diff --git a/key.go b/key.go
index 0302c29..2147974 100644
--- a/key.go
+++ b/key.go
@@ -113,6 +113,9 @@ func (k *Key) Value() string {
// ValueWithShadows returns raw values of key and its shadows if any.
func (k *Key) ValueWithShadows() []string {
if len(k.shadows) == 0 {
+ if k.value == "" {
+ return []string{}
+ }
return []string{k.value}
}
vals := make([]string, len(k.shadows)+1)
From 6ed3295bb951e2a12308a6a71541ee4dee554dfe Mon Sep 17 00:00:00 2001
From: Nicolai Dagestad
Date: Sat, 15 Jan 2022 16:22:32 +0100
Subject: [PATCH 2/7] Change the way of deciding if a value should be returned
or not
With the previous behaviour, many tests broke because it was not able to
distinguish between a key that was present but had no value and a key
that was not present at all.
---
key.go | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/key.go b/key.go
index 2147974..64e9206 100644
--- a/key.go
+++ b/key.go
@@ -112,10 +112,10 @@ func (k *Key) Value() string {
// ValueWithShadows returns raw values of key and its shadows if any.
func (k *Key) ValueWithShadows() []string {
+ if !k.s.HasKey(k.name) {
+ return []string{}
+ }
if len(k.shadows) == 0 {
- if k.value == "" {
- return []string{}
- }
return []string{k.value}
}
vals := make([]string, len(k.shadows)+1)
From ce6c1ab046b9b7996e144b53a3218fef7d2b61e5 Mon Sep 17 00:00:00 2001
From: Nicolai Dagestad
Date: Sat, 15 Jan 2022 16:25:04 +0100
Subject: [PATCH 3/7] Trailing whitespaces should not be trimed in test files
---
.editorconfig | 3 +++
1 file changed, 3 insertions(+)
diff --git a/.editorconfig b/.editorconfig
index 433be2a..4a2d918 100644
--- a/.editorconfig
+++ b/.editorconfig
@@ -7,3 +7,6 @@ charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
+
+[*_test.go]
+trim_trailing_whitespace = false
From 9fca1cd6b880b2455e741e08e298b6d5750fcc36 Mon Sep 17 00:00:00 2001
From: Nicolai Dagestad
Date: Sat, 15 Jan 2022 16:39:54 +0100
Subject: [PATCH 4/7] Add tests for ValueWithShadows
---
key_test.go | 19 +++++++++++++++++++
1 file changed, 19 insertions(+)
diff --git a/key_test.go b/key_test.go
index 5a842ad..ccd736c 100644
--- a/key_test.go
+++ b/key_test.go
@@ -521,6 +521,25 @@ func TestKey_Helpers(t *testing.T) {
})
}
+func testKey_ValueWithShadows(t *testing.T) {
+ t.Run("", func(t *testing.T) {
+ f, err := ShadowLoad([]byte(`
+keyName = value1
+keyName = value2
+`))
+ require.NoError(t, err)
+ require.NotNil(t, f)
+
+ k := f.Section("").Key("FakeKey")
+ require.NotNil(t, k)
+ assert.Equal(t, []string{}, k.ValueWithShadows())
+
+ k = f.Section("").Key("keyName")
+ require.NotNil(t, k)
+ assert.Equal(t, []string{"value1", "value2"}, k.ValueWithShadows())
+ })
+}
+
func TestKey_StringsWithShadows(t *testing.T) {
t.Run("get strings of shadows of a key", func(t *testing.T) {
f, err := ShadowLoad([]byte(""))
From 12524e3bbd5e91d4d97c764cd19e8c11d62d7aaa Mon Sep 17 00:00:00 2001
From: Nicolai Dagestad
Date: Mon, 17 Jan 2022 20:08:27 +0100
Subject: [PATCH 5/7] revert to the original decision behaviour for
ValueWithShadows
File.WriteToBuffer also needed to be tweaked so it could work with keys
that are present but have no value.
---
file.go | 16 +++++++++++++++-
key.go | 6 +++---
key_test.go | 2 +-
3 files changed, 19 insertions(+), 5 deletions(-)
diff --git a/file.go b/file.go
index 7b4e560..05dc282 100644
--- a/file.go
+++ b/file.go
@@ -442,7 +442,21 @@ func (f *File) writeToBuffer(indent string) (*bytes.Buffer, error) {
kname = `"""` + kname + `"""`
}
- for _, val := range key.ValueWithShadows() {
+ shadows := key.ValueWithShadows()
+ if len(shadows) == 0 {
+ if _, err := buf.WriteString(kname); err != nil {
+ return nil, err
+ }
+ // Write out alignment spaces before "=" sign
+ if PrettyFormat {
+ buf.Write(alignSpaces[:alignLength-len(kname)])
+ }
+ if _, err := buf.WriteString(equalSign + LineBreak); err != nil {
+ return nil, err
+ }
+ }
+
+ for _, val := range shadows {
if _, err := buf.WriteString(kname); err != nil {
return nil, err
}
diff --git a/key.go b/key.go
index 64e9206..2147974 100644
--- a/key.go
+++ b/key.go
@@ -112,10 +112,10 @@ func (k *Key) Value() string {
// ValueWithShadows returns raw values of key and its shadows if any.
func (k *Key) ValueWithShadows() []string {
- if !k.s.HasKey(k.name) {
- return []string{}
- }
if len(k.shadows) == 0 {
+ if k.value == "" {
+ return []string{}
+ }
return []string{k.value}
}
vals := make([]string, len(k.shadows)+1)
diff --git a/key_test.go b/key_test.go
index ccd736c..69ce2db 100644
--- a/key_test.go
+++ b/key_test.go
@@ -521,7 +521,7 @@ func TestKey_Helpers(t *testing.T) {
})
}
-func testKey_ValueWithShadows(t *testing.T) {
+func TestKey_ValueWithShadows(t *testing.T) {
t.Run("", func(t *testing.T) {
f, err := ShadowLoad([]byte(`
keyName = value1
From d77100b4d35d090db84f3295f78854620b9fd9b4 Mon Sep 17 00:00:00 2001
From: Nicolai Dagestad
Date: Tue, 18 Jan 2022 21:51:49 +0100
Subject: [PATCH 6/7] Extract the value writting logic to an function to
prevent code duplication in WriteToBuffer
---
file.go | 37 ++++++++++++++++++++-----------------
1 file changed, 20 insertions(+), 17 deletions(-)
diff --git a/file.go b/file.go
index 05dc282..f9d50c4 100644
--- a/file.go
+++ b/file.go
@@ -442,30 +442,16 @@ func (f *File) writeToBuffer(indent string) (*bytes.Buffer, error) {
kname = `"""` + kname + `"""`
}
- shadows := key.ValueWithShadows()
- if len(shadows) == 0 {
+ writeKeyValue := func(val string) (bool, error) {
if _, err := buf.WriteString(kname); err != nil {
- return nil, err
- }
- // Write out alignment spaces before "=" sign
- if PrettyFormat {
- buf.Write(alignSpaces[:alignLength-len(kname)])
- }
- if _, err := buf.WriteString(equalSign + LineBreak); err != nil {
- return nil, err
- }
- }
-
- for _, val := range shadows {
- if _, err := buf.WriteString(kname); err != nil {
- return nil, err
+ return false, err
}
if key.isBooleanType {
if kname != sec.keyList[len(sec.keyList)-1] {
buf.WriteString(LineBreak)
}
- continue KeyList
+ return true, nil
}
// Write out alignment spaces before "=" sign
@@ -482,10 +468,27 @@ func (f *File) writeToBuffer(indent string) (*bytes.Buffer, error) {
val = `"` + val + `"`
}
if _, err := buf.WriteString(equalSign + val + LineBreak); err != nil {
+ return false, err
+ }
+ return false, nil
+ }
+
+ shadows := key.ValueWithShadows()
+ if len(shadows) == 0 {
+ if _, err := writeKeyValue(""); err != nil {
return nil, err
}
}
+ for _, val := range shadows {
+ exit_loop, err := writeKeyValue(val)
+ if err != nil {
+ return nil, err
+ } else if exit_loop {
+ continue KeyList
+ }
+ }
+
for _, val := range key.nestedValues {
if _, err := buf.WriteString(indent + " " + val + LineBreak); err != nil {
return nil, err
From 3b0443860e9a45ac55af4a4a8b7067f8f86dfcee Mon Sep 17 00:00:00 2001
From: Joe Chen
Date: Thu, 20 Jan 2022 16:19:21 +0800
Subject: [PATCH 7/7] Update file.go
---
file.go | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/file.go b/file.go
index f9d50c4..9d91c31 100644
--- a/file.go
+++ b/file.go
@@ -481,10 +481,10 @@ func (f *File) writeToBuffer(indent string) (*bytes.Buffer, error) {
}
for _, val := range shadows {
- exit_loop, err := writeKeyValue(val)
+ exitLoop, err := writeKeyValue(val)
if err != nil {
return nil, err
- } else if exit_loop {
+ } else if exitLoop {
continue KeyList
}
}