Skip to content

Commit b3f0083

Browse files
committed
update
1 parent 8556217 commit b3f0083

3 files changed

Lines changed: 68 additions & 2 deletions

File tree

go-sdk/state/codec/default_codec.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,13 @@ func DefaultCodecFor[V any]() (Codec[V], error) {
4242
case reflect.String:
4343
return any(StringCodec{}).(Codec[V]), nil
4444
case reflect.Int:
45-
return any(Int64Codec{}).(Codec[V]), nil
45+
return any(IntCodec{}).(Codec[V]), nil
4646
case reflect.Int8:
4747
return any(Int8Codec{}).(Codec[V]), nil
4848
case reflect.Int16:
4949
return any(Int16Codec{}).(Codec[V]), nil
5050
case reflect.Uint:
51-
return any(Uint64Codec{}).(Codec[V]), nil
51+
return any(UintCodec{}).(Codec[V]), nil
5252
case reflect.Uint8:
5353
return any(Uint8Codec{}).(Codec[V]), nil
5454
case reflect.Uint16:

go-sdk/state/codec/int_codec.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Licensed under the Apache License, Version 2.0 (the "License");
2+
// you may not use this file except in compliance with the License.
3+
// You may obtain a copy of the License at
4+
//
5+
// http://www.apache.org/licenses/LICENSE-2.0
6+
//
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+
package codec
14+
15+
// IntCodec implements Codec[int] by delegating to Int64Codec.
16+
// It is used for reflect.Int (platform-sized int) so that DefaultCodecFor[int]()
17+
// returns a valid Codec[int] instead of panicking on type assertion.
18+
type IntCodec struct{}
19+
20+
var _ Codec[int] = IntCodec{}
21+
22+
func (c IntCodec) Encode(value int) ([]byte, error) {
23+
return Int64Codec{}.Encode(int64(value))
24+
}
25+
26+
func (c IntCodec) Decode(data []byte) (int, error) {
27+
v, err := Int64Codec{}.Decode(data)
28+
return int(v), err
29+
}
30+
31+
func (c IntCodec) EncodedSize() int { return 8 }
32+
33+
func (c IntCodec) IsOrderedKeyCodec() bool { return true }

go-sdk/state/codec/uint_codec.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Licensed under the Apache License, Version 2.0 (the "License");
2+
// you may not use this file except in compliance with the License.
3+
// You may obtain a copy of the License at
4+
//
5+
// http://www.apache.org/licenses/LICENSE-2.0
6+
//
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+
package codec
14+
15+
// UintCodec implements Codec[uint] by delegating to Uint64Codec.
16+
// It is used for reflect.Uint (platform-sized uint) so that DefaultCodecFor[uint]()
17+
// returns a valid Codec[uint] instead of panicking on type assertion.
18+
type UintCodec struct{}
19+
20+
var _ Codec[uint] = UintCodec{}
21+
22+
func (c UintCodec) Encode(value uint) ([]byte, error) {
23+
return Uint64Codec{}.Encode(uint64(value))
24+
}
25+
26+
func (c UintCodec) Decode(data []byte) (uint, error) {
27+
v, err := Uint64Codec{}.Decode(data)
28+
return uint(v), err
29+
}
30+
31+
func (c UintCodec) EncodedSize() int { return 8 }
32+
33+
func (c UintCodec) IsOrderedKeyCodec() bool { return true }

0 commit comments

Comments
 (0)