Skip to content

Commit be1a7ce

Browse files
committed
fix up
1 parent b293474 commit be1a7ce

File tree

2 files changed

+63
-38
lines changed

2 files changed

+63
-38
lines changed

components/usage/pkg/db/types.go

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -39,41 +39,34 @@ func (n *VarcharTime) Scan(value interface{}) error {
3939

4040
switch s := value.(type) {
4141
case []uint8:
42-
// Null value - empty string mean value is not set
43-
if len(s) == 0 {
44-
n.valid = false
45-
return nil
46-
}
47-
48-
parsed, err := iso8601.ParseString(string(s))
49-
if err != nil {
50-
return fmt.Errorf("failed to parse %v into ISO8601: %w", string(s), err)
51-
}
52-
53-
if parsed.IsZero() {
54-
n.valid = false
55-
return nil
56-
}
42+
return n.parseString(string(s))
43+
case string:
44+
return n.parseString(s)
45+
}
46+
return fmt.Errorf("unknown scan value for VarcharTime with value: %v", value)
47+
}
5748

58-
n.valid = true
59-
n.t = parsed.UTC()
49+
func (n *VarcharTime) parseString(s string) error {
50+
// Null value - empty string mean value is not set
51+
if len(s) == 0 {
52+
n.valid = false
6053
return nil
61-
case string:
62-
if len(s) == 0 {
63-
n.valid = false
64-
return nil
65-
}
54+
}
6655

67-
parsed, err := iso8601.ParseString(s)
68-
if err != nil {
69-
return fmt.Errorf("failed to parse %v into ISO8601: %w", s, err)
70-
}
56+
parsed, err := iso8601.ParseString(s)
57+
if err != nil {
58+
return fmt.Errorf("failed to parse %v into ISO8601: %w", s, err)
59+
}
7160

72-
n.valid = true
73-
n.t = parsed.UTC()
61+
if parsed.UTC().IsZero() {
62+
n.t = time.Time{}.UTC()
63+
n.valid = false
7464
return nil
7565
}
76-
return fmt.Errorf("unknown scan value for VarcharTime with value: %v", value)
66+
67+
n.valid = true
68+
n.t = parsed.UTC()
69+
return nil
7770
}
7871

7972
func (n VarcharTime) Time() time.Time {

components/usage/pkg/db/types_test.go

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package db
66

77
import (
88
"github.com/stretchr/testify/require"
9+
"gorm.io/gorm"
910
"testing"
1011
"time"
1112
)
@@ -64,15 +65,6 @@ func TestVarcharTime_Scan(t *testing.T) {
6465
},
6566
Error: false,
6667
},
67-
}, {
68-
Name: "zero timestamp is invalid",
69-
Input: "",
70-
Expected: Expectation{
71-
Time: VarcharTime{
72-
valid: false,
73-
},
74-
Error: false,
75-
},
7668
},
7769
} {
7870
t.Run(scenario.Name, func(t *testing.T) {
@@ -86,3 +78,43 @@ func TestVarcharTime_Scan(t *testing.T) {
8678
})
8779
}
8880
}
81+
82+
func TestVarcharTime_SerializeAndDeserialize(t *testing.T) {
83+
// Custom table to be able to exercise serialization easily, independent of other models
84+
type VarcharModel struct {
85+
ID int `gorm:"primaryKey"`
86+
Time VarcharTime `gorm:"column:time;type:varchar(255);"`
87+
}
88+
89+
conn := ConnectForTests(t)
90+
require.NoError(t, conn.AutoMigrate(&VarcharModel{}))
91+
92+
conn.Session(&gorm.Session{AllowGlobalUpdate: true}).Delete(&VarcharModel{})
93+
94+
for _, scenario := range []struct {
95+
Description string
96+
Input VarcharModel
97+
Expected VarcharModel
98+
}{
99+
{
100+
Description: "empty value for VarcharTime",
101+
Input: VarcharModel{
102+
ID: 1,
103+
Time: VarcharTime{},
104+
},
105+
Expected: VarcharModel{
106+
ID: 1,
107+
Time: VarcharTime{},
108+
},
109+
},
110+
} {
111+
tx := conn.Create(scenario.Input)
112+
require.NoError(t, tx.Error)
113+
114+
var read VarcharModel
115+
tx = conn.First(&read, scenario.Input.ID)
116+
require.NoError(t, tx.Error)
117+
118+
require.Equal(t, scenario.Expected, read)
119+
}
120+
}

0 commit comments

Comments
 (0)