mirror of
https://github.com/TomWright/dasel.git
synced 2022-05-22 02:32:45 +03:00
Merge pull request #212 from egawata/fix_toml_datetime
handle datetime values properly in .toml
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"github.com/pelletier/go-toml"
|
||||
"time"
|
||||
)
|
||||
|
||||
func init() {
|
||||
@@ -66,6 +67,8 @@ func (p *TOMLParser) ToBytes(value interface{}, options ...ReadWriteOption) ([]b
|
||||
}
|
||||
}
|
||||
}
|
||||
case time.Time:
|
||||
buf.Write([]byte(fmt.Sprintf("%s\n", d.Format(time.RFC3339))))
|
||||
default:
|
||||
if err := enc.Encode(d); err != nil {
|
||||
if err.Error() == "Only a struct or map can be marshaled to TOML" {
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
var tomlBytes = []byte(`names = ["John", "Frank"]
|
||||
@@ -133,6 +134,19 @@ func TestTOMLParser_ToBytes(t *testing.T) {
|
||||
}
|
||||
exp := `asd
|
||||
123
|
||||
`
|
||||
if exp != string(got) {
|
||||
t.Errorf("expected:\n%s\ngot:\n%s", exp, got)
|
||||
}
|
||||
})
|
||||
t.Run("time.Time", func(t *testing.T) {
|
||||
v, _ := time.Parse(time.RFC3339, "2022-01-02T12:34:56Z")
|
||||
got, err := (&storage.TOMLParser{}).ToBytes(v)
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %s", err)
|
||||
return
|
||||
}
|
||||
exp := `2022-01-02T12:34:56Z
|
||||
`
|
||||
if exp != string(got) {
|
||||
t.Errorf("expected:\n%s\ngot:\n%s", exp, got)
|
||||
|
||||
Reference in New Issue
Block a user