mirror of
https://github.com/getsops/sops.git
synced 2026-02-05 03:45:44 +01:00
Move ValToString to stores.
Signed-off-by: Felix Fontein <felix@fontein.de>
This commit is contained in:
@@ -4,9 +4,6 @@ import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/getsops/sops/v3"
|
||||
@@ -57,7 +54,7 @@ func (store Store) encodeTree(branches sops.TreeBranches) ([]byte, error) {
|
||||
lastItem.Comment = comment.Value
|
||||
}
|
||||
} else {
|
||||
lastItem, err = section.NewKey(keyVal.Key.(string), store.valToString(keyVal.Value))
|
||||
lastItem, err = section.NewKey(keyVal.Key.(string), stores.ValToString(keyVal.Value))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Error encoding key: %s", err)
|
||||
}
|
||||
@@ -79,26 +76,6 @@ func (store Store) stripCommentChar(comment string) string {
|
||||
return comment
|
||||
}
|
||||
|
||||
func (store Store) valToString(v interface{}) string {
|
||||
switch v := v.(type) {
|
||||
case float64:
|
||||
result := strconv.FormatFloat(v, 'G', -1, 64)
|
||||
// If the result can be confused with an integer, make sure we have at least one decimal digit
|
||||
if !strings.ContainsRune(result, '.') && !strings.ContainsRune(result, 'E') {
|
||||
result = strconv.FormatFloat(v, 'f', 1, 64)
|
||||
}
|
||||
return result
|
||||
case bool:
|
||||
return strconv.FormatBool(v)
|
||||
case time.Time:
|
||||
return v.Format(time.RFC3339)
|
||||
case fmt.Stringer:
|
||||
return v.String()
|
||||
default:
|
||||
return fmt.Sprintf("%v", v)
|
||||
}
|
||||
}
|
||||
|
||||
func (store Store) iniFromTreeBranches(branches sops.TreeBranches) ([]byte, error) {
|
||||
return store.encodeTree(branches)
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@ package ini
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/getsops/sops/v3"
|
||||
"github.com/stretchr/testify/assert"
|
||||
@@ -183,22 +182,3 @@ func TestUnmarshalMetadataFromNonSOPSFile(t *testing.T) {
|
||||
_, err := store.LoadEncryptedFile(data)
|
||||
assert.Equal(t, sops.MetadataNotFound, err)
|
||||
}
|
||||
|
||||
func TestValToString(t *testing.T) {
|
||||
store := Store{}
|
||||
assert.Equal(t, "1", store.valToString(1))
|
||||
assert.Equal(t, "1.0", store.valToString(1.0))
|
||||
assert.Equal(t, "1.1", store.valToString(1.10))
|
||||
assert.Equal(t, "1.23", store.valToString(1.23))
|
||||
assert.Equal(t, "1.2345678901234567", store.valToString(1.234567890123456789))
|
||||
assert.Equal(t, "200000.0", store.valToString(2E5))
|
||||
assert.Equal(t, "-2E+10", store.valToString(-2E10))
|
||||
assert.Equal(t, "2E-10", store.valToString(2E-10))
|
||||
assert.Equal(t, "1.2345E+100", store.valToString(1.2345E100))
|
||||
assert.Equal(t, "1.2345E-100", store.valToString(1.2345E-100))
|
||||
assert.Equal(t, "true", store.valToString(true))
|
||||
assert.Equal(t, "false", store.valToString(false))
|
||||
ts, _ := time.Parse(time.RFC3339, "2025-01-02T03:04:05Z")
|
||||
assert.Equal(t, "2025-01-02T03:04:05Z", store.valToString(ts))
|
||||
assert.Equal(t, "a string", store.valToString("a string"))
|
||||
}
|
||||
|
||||
@@ -10,9 +10,10 @@ of the purpose of this package is to make it easy to change the SOPS file format
|
||||
package stores
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/getsops/sops/v3"
|
||||
"github.com/getsops/sops/v3/age"
|
||||
@@ -533,3 +534,25 @@ func HasSopsTopLevelKey(branch sops.TreeBranch) bool {
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// ValToString converts a simple value to a string.
|
||||
// It does not handle complex values (arrays and mappings).
|
||||
func ValToString(v interface{}) string {
|
||||
switch v := v.(type) {
|
||||
case float64:
|
||||
result := strconv.FormatFloat(v, 'G', -1, 64)
|
||||
// If the result can be confused with an integer, make sure we have at least one decimal digit
|
||||
if !strings.ContainsRune(result, '.') && !strings.ContainsRune(result, 'E') {
|
||||
result = strconv.FormatFloat(v, 'f', 1, 64)
|
||||
}
|
||||
return result
|
||||
case bool:
|
||||
return strconv.FormatBool(v)
|
||||
case time.Time:
|
||||
return v.Format(time.RFC3339)
|
||||
case fmt.Stringer:
|
||||
return v.String()
|
||||
default:
|
||||
return fmt.Sprintf("%v", v)
|
||||
}
|
||||
}
|
||||
|
||||
27
stores/stores_test.go
Normal file
27
stores/stores_test.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package stores
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
|
||||
func TestValToString(t *testing.T) {
|
||||
assert.Equal(t, "1", ValToString(1))
|
||||
assert.Equal(t, "1.0", ValToString(1.0))
|
||||
assert.Equal(t, "1.1", ValToString(1.10))
|
||||
assert.Equal(t, "1.23", ValToString(1.23))
|
||||
assert.Equal(t, "1.2345678901234567", ValToString(1.234567890123456789))
|
||||
assert.Equal(t, "200000.0", ValToString(2E5))
|
||||
assert.Equal(t, "-2E+10", ValToString(-2E10))
|
||||
assert.Equal(t, "2E-10", ValToString(2E-10))
|
||||
assert.Equal(t, "1.2345E+100", ValToString(1.2345E100))
|
||||
assert.Equal(t, "1.2345E-100", ValToString(1.2345E-100))
|
||||
assert.Equal(t, "true", ValToString(true))
|
||||
assert.Equal(t, "false", ValToString(false))
|
||||
ts, _ := time.Parse(time.RFC3339, "2025-01-02T03:04:05Z")
|
||||
assert.Equal(t, "2025-01-02T03:04:05Z", ValToString(ts))
|
||||
assert.Equal(t, "a string", ValToString("a string"))
|
||||
}
|
||||
Reference in New Issue
Block a user