Skip to content

Commit 58654cd

Browse files
committed
add way to get configtype time value for hashing
1 parent dbdae2f commit 58654cd

File tree

4 files changed

+55
-0
lines changed

4 files changed

+55
-0
lines changed

configtype/time.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,11 @@ func (t Time) String() string {
130130
return t.input
131131
}
132132

133+
func (t Time) Hash() (uint64, error) {
134+
at := t.AsTime(time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC))
135+
return uint64(at.UnixNano()), nil
136+
}
137+
133138
type timeDuration struct {
134139
input string
135140

configtype/time_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/cloudquery/plugin-sdk/v4/configtype"
1010
"github.com/cloudquery/plugin-sdk/v4/plugin"
1111
"github.com/invopop/jsonschema"
12+
"github.com/mitchellh/hashstructure/v2"
1213
"github.com/stretchr/testify/require"
1314
)
1415

@@ -151,3 +152,49 @@ func TestTime_JSONSchema(t *testing.T) {
151152
})
152153
}
153154
}
155+
156+
func TestTime_Hashing(t *testing.T) {
157+
cases := []struct {
158+
a any
159+
b any
160+
equal bool
161+
}{
162+
{
163+
a: func() any { ct, _ := configtype.ParseTime("10m"); return ct }(),
164+
b: func() any { ct, _ := configtype.ParseTime("10m"); return ct }(),
165+
equal: true,
166+
},
167+
{
168+
a: func() any { ct, _ := configtype.ParseTime("10m"); return ct }(),
169+
b: func() any { ct, _ := configtype.ParseTime("1m"); return ct }(),
170+
equal: false,
171+
},
172+
{
173+
a: func() any { ct, _ := configtype.ParseTime("2021-09-01T00:00:00Z"); return ct }(),
174+
b: func() any { ct, _ := configtype.ParseTime("2012-01-02T01:02:03Z"); return ct }(),
175+
equal: false,
176+
},
177+
{
178+
a: func() any { ct, _ := configtype.ParseTime("-50m30s"); return ct }(),
179+
b: func() any { ct, _ := configtype.ParseTime("50 minutes 30 seconds ago"); return ct }(),
180+
equal: true,
181+
},
182+
{
183+
a: func() any { ct, _ := configtype.ParseTime("50m30s"); return ct }(),
184+
b: func() any { ct, _ := configtype.ParseTime("50 minutes 30 seconds from now"); return ct }(),
185+
equal: true,
186+
},
187+
}
188+
for _, tc := range cases {
189+
hashA, err := hashstructure.Hash(tc.a, hashstructure.FormatV2, nil)
190+
require.NoError(t, err)
191+
hashB, err := hashstructure.Hash(tc.b, hashstructure.FormatV2, nil)
192+
require.NoError(t, err)
193+
194+
if tc.equal {
195+
require.Equal(t, hashA, hashB)
196+
continue
197+
}
198+
require.NotEqual(t, hashA, hashB)
199+
}
200+
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ require (
2121
github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.2.0
2222
github.com/hashicorp/go-retryablehttp v0.7.7
2323
github.com/invopop/jsonschema v0.13.0
24+
github.com/mitchellh/hashstructure/v2 v2.0.2
2425
github.com/rs/zerolog v1.33.0
2526
github.com/samber/lo v1.47.0
2627
github.com/santhosh-tekuri/jsonschema/v6 v6.0.1

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ github.com/minio/asm2plan9s v0.0.0-20200509001527-cdd76441f9d8 h1:AMFGa4R4MiIpsp
125125
github.com/minio/asm2plan9s v0.0.0-20200509001527-cdd76441f9d8/go.mod h1:mC1jAcsrzbxHt8iiaC+zU4b1ylILSosueou12R++wfY=
126126
github.com/minio/c2goasm v0.0.0-20190812172519-36a3d3bbc4f3 h1:+n/aFZefKZp7spd8DFdX7uMikMLXX4oubIzJF4kv/wI=
127127
github.com/minio/c2goasm v0.0.0-20190812172519-36a3d3bbc4f3/go.mod h1:RagcQ7I8IeTMnF8JTXieKnO4Z6JCsikNEzj0DwauVzE=
128+
github.com/mitchellh/hashstructure/v2 v2.0.2 h1:vGKWl0YJqUNxE8d+h8f6NJLcCJrgbhC4NcD46KavDd4=
129+
github.com/mitchellh/hashstructure/v2 v2.0.2/go.mod h1:MG3aRVU/N29oo/V/IhBX8GR/zz4kQkprJgF2EVszyDE=
128130
github.com/oapi-codegen/runtime v1.1.1 h1:EXLHh0DXIJnWhdRPN2w4MXAzFyE4CskzhNLUmtpMYro=
129131
github.com/oapi-codegen/runtime v1.1.1/go.mod h1:SK9X900oXmPWilYR5/WKPzt3Kqxn/uS/+lbpREv+eCg=
130132
github.com/pierrec/lz4/v4 v4.1.22 h1:cKFw6uJDK+/gfw5BcDL0JL5aBsAFdsIT18eRtLj7VIU=

0 commit comments

Comments
 (0)