Skip to content

Commit 0e1fd37

Browse files
authored
Migrate metadata decoder from components-contrib to kit (#74)
* Migrate metadata decoder from components-contrib to kit Required creating the `utils` package for utils.IsTruthy too (ported from runtime) Signed-off-by: ItalyPaleAle <[email protected]> * Lint Signed-off-by: ItalyPaleAle <[email protected]> --------- Signed-off-by: ItalyPaleAle <[email protected]>
1 parent 2e939bc commit 0e1fd37

9 files changed

+1082
-1
lines changed

go.mod

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,23 @@ require (
99
github.com/lestrrat-go/jwx/v2 v2.0.15
1010
github.com/mitchellh/mapstructure v1.5.1-0.20220423185008-bf980b35cac4
1111
github.com/sirupsen/logrus v1.9.3
12+
github.com/spf13/cast v1.5.1
1213
github.com/stretchr/testify v1.8.4
1314
github.com/tidwall/transform v0.0.0-20201103190739-32f242e2dbde
1415
golang.org/x/crypto v0.14.0
1516
golang.org/x/exp v0.0.0-20231006140011-7918f672742d
1617
google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d
1718
google.golang.org/grpc v1.57.0
1819
google.golang.org/protobuf v1.31.0
20+
k8s.io/apimachinery v0.26.9
1921
k8s.io/utils v0.0.0-20230726121419-3b25d923346b
2022
)
2123

2224
require (
2325
github.com/davecgh/go-spew v1.1.1 // indirect
2426
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect
2527
github.com/goccy/go-json v0.10.2 // indirect
28+
github.com/gogo/protobuf v1.3.2 // indirect
2629
github.com/golang/protobuf v1.5.3 // indirect
2730
github.com/lestrrat-go/blackmagic v1.0.2 // indirect
2831
github.com/lestrrat-go/httpcc v1.0.1 // indirect
@@ -31,5 +34,6 @@ require (
3134
github.com/pmezard/go-difflib v1.0.0 // indirect
3235
github.com/segmentio/asm v1.2.0 // indirect
3336
golang.org/x/sys v0.13.0 // indirect
37+
gopkg.in/inf.v0 v0.9.1 // indirect
3438
gopkg.in/yaml.v3 v3.0.1 // indirect
3539
)

go.sum

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,23 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
66
github.com/decred/dcrd/crypto/blake256 v1.0.1/go.mod h1:2OfgNZ5wDpcsFmHmCK5gZTPcCXqlm2ArzUIkw9czNJo=
77
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 h1:8UrgZ3GkP4i/CLijOJx79Yu+etlyjdBU4sfcs2WYQMs=
88
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0/go.mod h1:v57UDF4pDQJcEfFUCRop3lJL149eHGSe9Jvczhzjo/0=
9+
github.com/frankban/quicktest v1.14.4 h1:g2rn0vABPOOXmZUj+vbmUp0lPoXEMuhTpIluN0XL9UY=
910
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
1011
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
1112
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
1213
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
14+
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
15+
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
1316
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
1417
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
1518
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
1619
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
1720
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
21+
github.com/google/gofuzz v1.1.0 h1:Hsa8mG0dQ46ij8Sl2AYJDUv1oA9/d6Vk+3LG99Oe02g=
22+
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
23+
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
24+
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
25+
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
1826
github.com/lestrrat-go/blackmagic v1.0.2 h1:Cg2gVSc9h7sz9NOByczrbUvLopQmXrfFx//N+AkAr5k=
1927
github.com/lestrrat-go/blackmagic v1.0.2/go.mod h1:UrEqBzIR2U6CnzVyUtfM6oZNMt/7O7Vohk2J0OGSAtU=
2028
github.com/lestrrat-go/httpcc v1.0.1 h1:ydWCStUeJLkpYyjLDHihupbn2tYmZ7m22BGkcvZZrIE=
@@ -30,12 +38,17 @@ github.com/lestrrat-go/option v1.0.1 h1:oAzP2fvZGQKWkvHa1/SAcFolBEca1oN+mQ7eooNB
3038
github.com/lestrrat-go/option v1.0.1/go.mod h1:5ZHFbivi4xwXxhxY9XHDe2FHo6/Z7WWmtT7T5nBBp3I=
3139
github.com/mitchellh/mapstructure v1.5.1-0.20220423185008-bf980b35cac4 h1:BpfhmLKZf+SjVanKKhCgf3bg+511DmU9eDQTen7LLbY=
3240
github.com/mitchellh/mapstructure v1.5.1-0.20220423185008-bf980b35cac4/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
41+
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
3342
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
3443
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
44+
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
3545
github.com/segmentio/asm v1.2.0 h1:9BQrFxC+YOHJlTlHGkTrFWf59nbL3XnCoFLTwDCI7ys=
3646
github.com/segmentio/asm v1.2.0/go.mod h1:BqMnlJP91P8d+4ibuonYZw9mfnzI9HfxselHZr5aAcs=
3747
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
3848
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
49+
github.com/spf13/cast v1.5.1 h1:R+kOtfhWQE6TVQzY+4D7wJLBgkdVasCEFxSUBYBYIlA=
50+
github.com/spf13/cast v1.5.1/go.mod h1:b9PdjNptOpzXr7Rq1q9gJML/2cdGQAo69NKzQ10KN48=
51+
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
3952
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
4053
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
4154
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
@@ -47,25 +60,38 @@ github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcU
4760
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
4861
github.com/tidwall/transform v0.0.0-20201103190739-32f242e2dbde h1:AMNpJRc7P+GTwVbl8DkK2I9I8BBUzNiHuH/tlxrpan0=
4962
github.com/tidwall/transform v0.0.0-20201103190739-32f242e2dbde/go.mod h1:MvrEmduDUz4ST5pGZ7CABCnOU5f3ZiOAZzT6b1A6nX8=
63+
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
64+
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
5065
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
5166
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
67+
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
68+
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
5269
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
5370
golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc=
5471
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
5572
golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI=
5673
golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo=
74+
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
75+
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
5776
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
5877
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
78+
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
5979
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
80+
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
81+
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
6082
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
6183
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
6284
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
6385
golang.org/x/net v0.10.0 h1:X2//UzNDwYmtCLn7To6G58Wr6f5ahEAQgKNzv9Y951M=
6486
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
6587
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
88+
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
89+
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
6690
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
6791
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
6892
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
93+
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
94+
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
6995
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
7096
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
7197
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
@@ -90,10 +116,14 @@ golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
90116
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
91117
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
92118
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
119+
golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
120+
golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
93121
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
94122
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
95123
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
124+
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
96125
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
126+
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
97127
google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d h1:uvYuEyMHKNt+lT4K3bN6fGswmK8qSvcreM3BwjDh+y4=
98128
google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d/go.mod h1:+Bk1OCOj40wS2hwAMA+aCW9ypzm63QTBBHp6lQ3p+9M=
99129
google.golang.org/grpc v1.57.0 h1:kfzNeI/klCGD2YPMUlaGNT3pxvYfga7smW3Vth8Zsiw=
@@ -102,10 +132,14 @@ google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp0
102132
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
103133
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
104134
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
105-
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
106135
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
136+
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU=
137+
gopkg.in/inf.v0 v0.9.1 h1:73M5CoZyi3ZLMOyDlQh031Cx6N9NDJ2Vvfl76EDAgDc=
138+
gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw=
107139
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
108140
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
109141
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
142+
k8s.io/apimachinery v0.26.9 h1:5yAV9cFR7Z4gIorKcAjWnx4uxtxiFsERwq4Pvmx0CCg=
143+
k8s.io/apimachinery v0.26.9/go.mod h1:qYzLkrQ9lhrZRh0jNKo2cfvf/R1/kQONnSiyB7NUJU0=
110144
k8s.io/utils v0.0.0-20230726121419-3b25d923346b h1:sgn3ZU783SCgtaSJjpcVVlRqd6GSnlTLKgpAAttJvpI=
111145
k8s.io/utils v0.0.0-20230726121419-3b25d923346b/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=

metadata/bytesize_decoder.go

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
Copyright 2023 The Dapr Authors
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
package metadata
15+
16+
import (
17+
"fmt"
18+
"reflect"
19+
20+
"github.com/mitchellh/mapstructure"
21+
"github.com/spf13/cast"
22+
"k8s.io/apimachinery/pkg/api/resource"
23+
)
24+
25+
// ByteSize contains a quantity for a resource that is measured in bytes.
26+
// This extends the resource.Quantity struct from k8s.io/apimachinery to add some utility methods specific for Dapr.
27+
// Although the library from K8s supports other kinds of resource quantities, our focus is on sizes in bytes.
28+
type ByteSize struct {
29+
resource.Quantity
30+
}
31+
32+
// NewByteSize returns a new ByteSize with a default value in bytes.
33+
func NewByteSize(defaultValue int64) ByteSize {
34+
return ByteSize{
35+
Quantity: *resource.NewQuantity(defaultValue, resource.BinarySI),
36+
}
37+
}
38+
39+
// GetBytes returns the number of bytes in the quantity.
40+
// Note: this operation is expensive, so it's recommended to cache the returned value.
41+
func (q *ByteSize) GetBytes() (int64, error) {
42+
if q == nil || q.IsZero() {
43+
return 0, nil
44+
}
45+
46+
val, ok := q.AsInt64()
47+
if !ok {
48+
return 0, fmt.Errorf("cannot get bytes from resource quantity value '%v'", q)
49+
}
50+
51+
return val, nil
52+
}
53+
54+
func toByteSizeHookFunc() mapstructure.DecodeHookFunc {
55+
bytesizeType := reflect.TypeOf(ByteSize{})
56+
bytesizePtrType := reflect.TypeOf(&ByteSize{})
57+
58+
return func(
59+
f reflect.Type,
60+
t reflect.Type,
61+
data any,
62+
) (any, error) {
63+
var isPtr bool
64+
switch t {
65+
case bytesizeType:
66+
// Nop
67+
case bytesizePtrType:
68+
isPtr = true
69+
default:
70+
// Not a type we support with this hook
71+
return data, nil
72+
}
73+
74+
// First, cast to string
75+
str, err := cast.ToStringE(data)
76+
if err != nil {
77+
return nil, fmt.Errorf("failed to cast value to string: %w", err)
78+
}
79+
80+
// Parse as quantity
81+
q, err := resource.ParseQuantity(str)
82+
if err != nil {
83+
return nil, fmt.Errorf("value is not a valid quantity: %w", err)
84+
}
85+
86+
// Return a pointer if desired
87+
res := ByteSize{Quantity: q}
88+
if isPtr {
89+
return &res, nil
90+
}
91+
return res, nil
92+
}
93+
}

metadata/duration.go

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
/*
2+
Copyright 2023 The Dapr Authors
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
package metadata
15+
16+
// JSON marshaling and unmarshaling methods for time.Duration based on https://stackoverflow.com/a/48051946
17+
// Includes methods to return an ISO-8601 formatted string from a time.Duration.
18+
import (
19+
"encoding/json"
20+
"errors"
21+
"reflect"
22+
"strconv"
23+
"time"
24+
25+
"github.com/mitchellh/mapstructure"
26+
)
27+
28+
type Duration struct {
29+
time.Duration
30+
}
31+
32+
func (d Duration) MarshalJSON() ([]byte, error) {
33+
return json.Marshal(d.String())
34+
}
35+
36+
func (d *Duration) UnmarshalJSON(b []byte) error {
37+
var v any
38+
if err := json.Unmarshal(b, &v); err != nil {
39+
return err
40+
}
41+
switch value := v.(type) {
42+
case float64:
43+
d.Duration = time.Duration(value)
44+
return nil
45+
case string:
46+
var err error
47+
d.Duration, err = time.ParseDuration(value)
48+
return err
49+
default:
50+
return errors.New("invalid duration")
51+
}
52+
}
53+
54+
// This helper function is used to decode durations within a map[string]any into a struct.
55+
// It must be used in conjunction with mapstructure's DecodeHook.
56+
// This is used in utils.DecodeMetadata to decode durations in metadata.
57+
//
58+
// mapstructure.NewDecoder(&mapstructure.DecoderConfig{
59+
// DecodeHook: mapstructure.ComposeDecodeHookFunc(
60+
// toTimeDurationHookFunc()),
61+
// Metadata: nil,
62+
// Result: result,
63+
// })
64+
func toTimeDurationHookFunc() mapstructure.DecodeHookFunc {
65+
return func(
66+
f reflect.Type,
67+
t reflect.Type,
68+
data any,
69+
) (any, error) {
70+
if t != reflect.TypeOf(Duration{}) && t != reflect.TypeOf(time.Duration(0)) {
71+
return data, nil
72+
}
73+
74+
switch f.Kind() {
75+
case reflect.TypeOf(time.Duration(0)).Kind():
76+
return data.(time.Duration), nil
77+
case reflect.String:
78+
var val time.Duration
79+
if data.(string) != "" {
80+
var err error
81+
val, err = time.ParseDuration(data.(string))
82+
if err != nil {
83+
// If we can't parse the duration, try parsing it as int64 seconds
84+
seconds, errParse := strconv.ParseInt(data.(string), 10, 0)
85+
if errParse != nil {
86+
return nil, errors.Join(err, errParse)
87+
}
88+
val = time.Duration(seconds * int64(time.Second))
89+
}
90+
}
91+
if t != reflect.TypeOf(Duration{}) {
92+
return val, nil
93+
}
94+
return Duration{Duration: val}, nil
95+
case reflect.Float64:
96+
val := time.Duration(data.(float64))
97+
if t != reflect.TypeOf(Duration{}) {
98+
return val, nil
99+
}
100+
return Duration{Duration: val}, nil
101+
case reflect.Int64:
102+
val := time.Duration(data.(int64))
103+
if t != reflect.TypeOf(Duration{}) {
104+
return val, nil
105+
}
106+
return Duration{Duration: val}, nil
107+
default:
108+
return data, nil
109+
}
110+
}
111+
}
112+
113+
// ToISOString returns the duration formatted as a ISO-8601 duration string (-ish).
114+
// This methods supports days, hours, minutes, and seconds. It assumes all durations are in UTC time and are not impacted by DST (so all days are 24-hours long).
115+
// This method does not support fractions of seconds, and durations are truncated to seconds.
116+
// See https://en.wikipedia.org/wiki/ISO_8601#Durations for referece.
117+
func (d Duration) ToISOString() string {
118+
// Truncate to seconds, removing fractional seconds
119+
trunc := d.Truncate(time.Second)
120+
121+
seconds := int64(trunc.Seconds())
122+
if seconds == 0 {
123+
// Zero value
124+
return "P0D"
125+
}
126+
127+
res := "P"
128+
if seconds >= 86400 {
129+
res += strconv.FormatInt(seconds/86400, 10) + "D"
130+
seconds %= 86400
131+
}
132+
if seconds == 0 {
133+
// Short-circuit if there's nothing left (we had whole days only)
134+
return res
135+
}
136+
res += "T"
137+
if seconds >= 3600 {
138+
res += strconv.FormatInt(seconds/3600, 10) + "H"
139+
seconds %= 3600
140+
}
141+
if seconds >= 60 {
142+
res += strconv.FormatInt(seconds/60, 10) + "M"
143+
seconds %= 60
144+
}
145+
if seconds > 0 {
146+
res += strconv.FormatInt(seconds, 10) + "S"
147+
}
148+
return res
149+
}

0 commit comments

Comments
 (0)