Skip to content

Commit 66f260b

Browse files
committed
Move ValToString to stores.
Signed-off-by: Felix Fontein <[email protected]>
1 parent 2d12e24 commit 66f260b

File tree

4 files changed

+53
-46
lines changed

4 files changed

+53
-46
lines changed

stores/ini/store.go

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@ import (
44
"bytes"
55
"encoding/json"
66
"fmt"
7-
"time"
8-
9-
"strconv"
107
"strings"
118

129
"github.com/getsops/sops/v3"
@@ -57,7 +54,7 @@ func (store Store) encodeTree(branches sops.TreeBranches) ([]byte, error) {
5754
lastItem.Comment = comment.Value
5855
}
5956
} else {
60-
lastItem, err = section.NewKey(keyVal.Key.(string), store.valToString(keyVal.Value))
57+
lastItem, err = section.NewKey(keyVal.Key.(string), stores.ValToString(keyVal.Value))
6158
if err != nil {
6259
return nil, fmt.Errorf("Error encoding key: %s", err)
6360
}
@@ -79,26 +76,6 @@ func (store Store) stripCommentChar(comment string) string {
7976
return comment
8077
}
8178

82-
func (store Store) valToString(v interface{}) string {
83-
switch v := v.(type) {
84-
case float64:
85-
result := strconv.FormatFloat(v, 'G', -1, 64)
86-
// If the result can be confused with an integer, make sure we have at least one decimal digit
87-
if !strings.ContainsRune(result, '.') && !strings.ContainsRune(result, 'E') {
88-
result = strconv.FormatFloat(v, 'f', 1, 64)
89-
}
90-
return result
91-
case bool:
92-
return strconv.FormatBool(v)
93-
case time.Time:
94-
return v.Format(time.RFC3339)
95-
case fmt.Stringer:
96-
return v.String()
97-
default:
98-
return fmt.Sprintf("%v", v)
99-
}
100-
}
101-
10279
func (store Store) iniFromTreeBranches(branches sops.TreeBranches) ([]byte, error) {
10380
return store.encodeTree(branches)
10481
}

stores/ini/store_test.go

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package ini
22

33
import (
44
"testing"
5-
"time"
65

76
"github.com/getsops/sops/v3"
87
"github.com/stretchr/testify/assert"
@@ -183,22 +182,3 @@ func TestUnmarshalMetadataFromNonSOPSFile(t *testing.T) {
183182
_, err := store.LoadEncryptedFile(data)
184183
assert.Equal(t, sops.MetadataNotFound, err)
185184
}
186-
187-
func TestValToString(t *testing.T) {
188-
store := Store{}
189-
assert.Equal(t, "1", store.valToString(1))
190-
assert.Equal(t, "1.0", store.valToString(1.0))
191-
assert.Equal(t, "1.1", store.valToString(1.10))
192-
assert.Equal(t, "1.23", store.valToString(1.23))
193-
assert.Equal(t, "1.2345678901234567", store.valToString(1.234567890123456789))
194-
assert.Equal(t, "200000.0", store.valToString(2E5))
195-
assert.Equal(t, "-2E+10", store.valToString(-2E10))
196-
assert.Equal(t, "2E-10", store.valToString(2E-10))
197-
assert.Equal(t, "1.2345E+100", store.valToString(1.2345E100))
198-
assert.Equal(t, "1.2345E-100", store.valToString(1.2345E-100))
199-
assert.Equal(t, "true", store.valToString(true))
200-
assert.Equal(t, "false", store.valToString(false))
201-
ts, _ := time.Parse(time.RFC3339, "2025-01-02T03:04:05Z")
202-
assert.Equal(t, "2025-01-02T03:04:05Z", store.valToString(ts))
203-
assert.Equal(t, "a string", store.valToString("a string"))
204-
}

stores/stores.go

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ of the purpose of this package is to make it easy to change the SOPS file format
1010
package stores
1111

1212
import (
13-
"time"
14-
1513
"fmt"
14+
"strconv"
15+
"strings"
16+
"time"
1617

1718
"github.com/getsops/sops/v3"
1819
"github.com/getsops/sops/v3/age"
@@ -533,3 +534,25 @@ func HasSopsTopLevelKey(branch sops.TreeBranch) bool {
533534
}
534535
return false
535536
}
537+
538+
// ValToString converts a simple value to a string.
539+
// It does not handle complex values (arrays and mappings).
540+
func ValToString(v interface{}) string {
541+
switch v := v.(type) {
542+
case float64:
543+
result := strconv.FormatFloat(v, 'G', -1, 64)
544+
// If the result can be confused with an integer, make sure we have at least one decimal digit
545+
if !strings.ContainsRune(result, '.') && !strings.ContainsRune(result, 'E') {
546+
result = strconv.FormatFloat(v, 'f', 1, 64)
547+
}
548+
return result
549+
case bool:
550+
return strconv.FormatBool(v)
551+
case time.Time:
552+
return v.Format(time.RFC3339)
553+
case fmt.Stringer:
554+
return v.String()
555+
default:
556+
return fmt.Sprintf("%v", v)
557+
}
558+
}

stores/stores_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package stores
2+
3+
import (
4+
"testing"
5+
"time"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
11+
func TestValToString(t *testing.T) {
12+
assert.Equal(t, "1", ValToString(1))
13+
assert.Equal(t, "1.0", ValToString(1.0))
14+
assert.Equal(t, "1.1", ValToString(1.10))
15+
assert.Equal(t, "1.23", ValToString(1.23))
16+
assert.Equal(t, "1.2345678901234567", ValToString(1.234567890123456789))
17+
assert.Equal(t, "200000.0", ValToString(2E5))
18+
assert.Equal(t, "-2E+10", ValToString(-2E10))
19+
assert.Equal(t, "2E-10", ValToString(2E-10))
20+
assert.Equal(t, "1.2345E+100", ValToString(1.2345E100))
21+
assert.Equal(t, "1.2345E-100", ValToString(1.2345E-100))
22+
assert.Equal(t, "true", ValToString(true))
23+
assert.Equal(t, "false", ValToString(false))
24+
ts, _ := time.Parse(time.RFC3339, "2025-01-02T03:04:05Z")
25+
assert.Equal(t, "2025-01-02T03:04:05Z", ValToString(ts))
26+
assert.Equal(t, "a string", ValToString("a string"))
27+
}

0 commit comments

Comments
 (0)