Skip to content

Commit

Permalink
add way to get configtype time value for hashing
Browse files Browse the repository at this point in the history
  • Loading branch information
blesniewski committed Feb 11, 2025
1 parent dbdae2f commit 58654cd
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 0 deletions.
5 changes: 5 additions & 0 deletions configtype/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ func (t Time) String() string {
return t.input
}

func (t Time) Hash() (uint64, error) {
at := t.AsTime(time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC))
return uint64(at.UnixNano()), nil
}

type timeDuration struct {
input string

Expand Down
47 changes: 47 additions & 0 deletions configtype/time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/cloudquery/plugin-sdk/v4/configtype"
"github.com/cloudquery/plugin-sdk/v4/plugin"
"github.com/invopop/jsonschema"
"github.com/mitchellh/hashstructure/v2"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -151,3 +152,49 @@ func TestTime_JSONSchema(t *testing.T) {
})
}
}

func TestTime_Hashing(t *testing.T) {
cases := []struct {
a any
b any
equal bool
}{
{
a: func() any { ct, _ := configtype.ParseTime("10m"); return ct }(),
b: func() any { ct, _ := configtype.ParseTime("10m"); return ct }(),
equal: true,
},
{
a: func() any { ct, _ := configtype.ParseTime("10m"); return ct }(),
b: func() any { ct, _ := configtype.ParseTime("1m"); return ct }(),
equal: false,
},
{
a: func() any { ct, _ := configtype.ParseTime("2021-09-01T00:00:00Z"); return ct }(),
b: func() any { ct, _ := configtype.ParseTime("2012-01-02T01:02:03Z"); return ct }(),
equal: false,
},
{
a: func() any { ct, _ := configtype.ParseTime("-50m30s"); return ct }(),
b: func() any { ct, _ := configtype.ParseTime("50 minutes 30 seconds ago"); return ct }(),
equal: true,
},
{
a: func() any { ct, _ := configtype.ParseTime("50m30s"); return ct }(),
b: func() any { ct, _ := configtype.ParseTime("50 minutes 30 seconds from now"); return ct }(),
equal: true,
},
}
for _, tc := range cases {
hashA, err := hashstructure.Hash(tc.a, hashstructure.FormatV2, nil)
require.NoError(t, err)
hashB, err := hashstructure.Hash(tc.b, hashstructure.FormatV2, nil)
require.NoError(t, err)

if tc.equal {
require.Equal(t, hashA, hashB)
continue
}
require.NotEqual(t, hashA, hashB)
}
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ require (
github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.2.0
github.com/hashicorp/go-retryablehttp v0.7.7
github.com/invopop/jsonschema v0.13.0
github.com/mitchellh/hashstructure/v2 v2.0.2
github.com/rs/zerolog v1.33.0
github.com/samber/lo v1.47.0
github.com/santhosh-tekuri/jsonschema/v6 v6.0.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ github.com/minio/asm2plan9s v0.0.0-20200509001527-cdd76441f9d8 h1:AMFGa4R4MiIpsp
github.com/minio/asm2plan9s v0.0.0-20200509001527-cdd76441f9d8/go.mod h1:mC1jAcsrzbxHt8iiaC+zU4b1ylILSosueou12R++wfY=
github.com/minio/c2goasm v0.0.0-20190812172519-36a3d3bbc4f3 h1:+n/aFZefKZp7spd8DFdX7uMikMLXX4oubIzJF4kv/wI=
github.com/minio/c2goasm v0.0.0-20190812172519-36a3d3bbc4f3/go.mod h1:RagcQ7I8IeTMnF8JTXieKnO4Z6JCsikNEzj0DwauVzE=
github.com/mitchellh/hashstructure/v2 v2.0.2 h1:vGKWl0YJqUNxE8d+h8f6NJLcCJrgbhC4NcD46KavDd4=
github.com/mitchellh/hashstructure/v2 v2.0.2/go.mod h1:MG3aRVU/N29oo/V/IhBX8GR/zz4kQkprJgF2EVszyDE=
github.com/oapi-codegen/runtime v1.1.1 h1:EXLHh0DXIJnWhdRPN2w4MXAzFyE4CskzhNLUmtpMYro=
github.com/oapi-codegen/runtime v1.1.1/go.mod h1:SK9X900oXmPWilYR5/WKPzt3Kqxn/uS/+lbpREv+eCg=
github.com/pierrec/lz4/v4 v4.1.22 h1:cKFw6uJDK+/gfw5BcDL0JL5aBsAFdsIT18eRtLj7VIU=
Expand Down

0 comments on commit 58654cd

Please sign in to comment.