Local time support (#318)
This commit is contained in:
+2
-1
@@ -69,6 +69,7 @@ const (
|
|||||||
var timeType = reflect.TypeOf(time.Time{})
|
var timeType = reflect.TypeOf(time.Time{})
|
||||||
var marshalerType = reflect.TypeOf(new(Marshaler)).Elem()
|
var marshalerType = reflect.TypeOf(new(Marshaler)).Elem()
|
||||||
var localDateType = reflect.TypeOf(LocalDate{})
|
var localDateType = reflect.TypeOf(LocalDate{})
|
||||||
|
var localTimeType = reflect.TypeOf(LocalTime{})
|
||||||
var localDateTimeType = reflect.TypeOf(LocalDateTime{})
|
var localDateTimeType = reflect.TypeOf(LocalDateTime{})
|
||||||
|
|
||||||
// Check if the given marshal type maps to a Tree primitive
|
// Check if the given marshal type maps to a Tree primitive
|
||||||
@@ -87,7 +88,7 @@ func isPrimitive(mtype reflect.Type) bool {
|
|||||||
case reflect.String:
|
case reflect.String:
|
||||||
return true
|
return true
|
||||||
case reflect.Struct:
|
case reflect.Struct:
|
||||||
return mtype == timeType || mtype == localDateType || mtype == localDateTimeType || isCustomMarshaler(mtype)
|
return mtype == timeType || mtype == localDateType || mtype == localDateTimeType || mtype == localTimeType || isCustomMarshaler(mtype)
|
||||||
default:
|
default:
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|||||||
+106
-4
@@ -1959,16 +1959,16 @@ func TestUnmarshalLocalDateTime(t *testing.T) {
|
|||||||
t.Errorf("expected day %d, got %d", example.out.Date.Day, obj.Date.Day())
|
t.Errorf("expected day %d, got %d", example.out.Date.Day, obj.Date.Day())
|
||||||
}
|
}
|
||||||
if obj.Date.Hour() != example.out.Time.Hour {
|
if obj.Date.Hour() != example.out.Time.Hour {
|
||||||
t.Errorf("expected day %d, got %d", example.out.Time.Hour, obj.Date.Hour())
|
t.Errorf("expected hour %d, got %d", example.out.Time.Hour, obj.Date.Hour())
|
||||||
}
|
}
|
||||||
if obj.Date.Minute() != example.out.Time.Minute {
|
if obj.Date.Minute() != example.out.Time.Minute {
|
||||||
t.Errorf("expected day %d, got %d", example.out.Time.Minute, obj.Date.Minute())
|
t.Errorf("expected minute %d, got %d", example.out.Time.Minute, obj.Date.Minute())
|
||||||
}
|
}
|
||||||
if obj.Date.Second() != example.out.Time.Second {
|
if obj.Date.Second() != example.out.Time.Second {
|
||||||
t.Errorf("expected day %d, got %d", example.out.Time.Second, obj.Date.Second())
|
t.Errorf("expected second %d, got %d", example.out.Time.Second, obj.Date.Second())
|
||||||
}
|
}
|
||||||
if obj.Date.Nanosecond() != example.out.Time.Nanosecond {
|
if obj.Date.Nanosecond() != example.out.Time.Nanosecond {
|
||||||
t.Errorf("expected day %d, got %d", example.out.Time.Nanosecond, obj.Date.Nanosecond())
|
t.Errorf("expected nanoseconds %d, got %d", example.out.Time.Nanosecond, obj.Date.Nanosecond())
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -2038,3 +2038,105 @@ func TestMarshalLocalDateTime(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestUnmarshalLocalTime(t *testing.T) {
|
||||||
|
examples := []struct {
|
||||||
|
name string
|
||||||
|
in string
|
||||||
|
out LocalTime
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "normal",
|
||||||
|
in: "07:32:00",
|
||||||
|
out: LocalTime{
|
||||||
|
Hour: 7,
|
||||||
|
Minute: 32,
|
||||||
|
Second: 0,
|
||||||
|
Nanosecond: 0,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "with nanoseconds",
|
||||||
|
in: "00:32:00.999999",
|
||||||
|
out: LocalTime{
|
||||||
|
Hour: 0,
|
||||||
|
Minute: 32,
|
||||||
|
Second: 0,
|
||||||
|
Nanosecond: 999999000,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, example := range examples {
|
||||||
|
toml := fmt.Sprintf(`Time = %s`, example.in)
|
||||||
|
|
||||||
|
t.Run(fmt.Sprintf("ToLocalTime_%d_%s", i, example.name), func(t *testing.T) {
|
||||||
|
type dateStruct struct {
|
||||||
|
Time LocalTime
|
||||||
|
}
|
||||||
|
|
||||||
|
var obj dateStruct
|
||||||
|
|
||||||
|
err := Unmarshal([]byte(toml), &obj)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if obj.Time != example.out {
|
||||||
|
t.Errorf("expected '%s', got '%s'", example.out, obj.Time)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMarshalLocalTime(t *testing.T) {
|
||||||
|
type timeStruct struct {
|
||||||
|
Time LocalTime
|
||||||
|
}
|
||||||
|
|
||||||
|
examples := []struct {
|
||||||
|
name string
|
||||||
|
in LocalTime
|
||||||
|
out string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "normal",
|
||||||
|
out: "Time = 07:32:00\n",
|
||||||
|
in: LocalTime{
|
||||||
|
Hour: 7,
|
||||||
|
Minute: 32,
|
||||||
|
Second: 0,
|
||||||
|
Nanosecond: 0,
|
||||||
|
}},
|
||||||
|
{
|
||||||
|
name: "with nanoseconds",
|
||||||
|
out: "Time = 00:32:00.999999000\n",
|
||||||
|
in: LocalTime{
|
||||||
|
Hour: 0,
|
||||||
|
Minute: 32,
|
||||||
|
Second: 0,
|
||||||
|
Nanosecond: 999999000,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, example := range examples {
|
||||||
|
t.Run(fmt.Sprintf("%d_%s", i, example.name), func(t *testing.T) {
|
||||||
|
obj := timeStruct{
|
||||||
|
Time: example.in,
|
||||||
|
}
|
||||||
|
b, err := Marshal(obj)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("unexpected error: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
got := string(b)
|
||||||
|
|
||||||
|
if got != example.out {
|
||||||
|
t.Errorf("expected '%s', got '%s'", example.out, got)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -140,6 +140,8 @@ func tomlValueStringRepresentation(v interface{}, indent string, arraysOneElemen
|
|||||||
return value.String(), nil
|
return value.String(), nil
|
||||||
case LocalDateTime:
|
case LocalDateTime:
|
||||||
return value.String(), nil
|
return value.String(), nil
|
||||||
|
case LocalTime:
|
||||||
|
return value.String(), nil
|
||||||
case nil:
|
case nil:
|
||||||
return "", nil
|
return "", nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user