chengxilo commented on code in PR #1995:
URL: https://github.com/apache/iggy/pull/1995#discussion_r2201253065
##########
foreign/go/contracts/identifier.go:
##########
@@ -30,22 +32,45 @@ const (
StringId IdKind = 2
)
-func NewIdentifier(id any) Identifier {
- var kind IdKind
- var length int
-
- switch v := id.(type) {
- case int:
- kind = NumericId
- length = 4
- case string:
- kind = StringId
- length = len(v)
+// NewNumericIdentifier creates a new identifier from the given numeric value.
+func NewNumericIdentifier(value uint32) (Identifier, error) {
+ if value == 0 {
+ return Identifier{}, ierror.InvalidIdentifier
}
+ return Identifier{
+ Kind: NumericId,
+ Length: 4,
+ Value: value,
+ }, nil
+}
+// NewStringIdentifier creates a new identifier from the given string value.
+func NewStringIdentifier(value string) (Identifier, error) {
+ length := len(value)
+ if length == 0 || length > 255 {
+ return Identifier{}, ierror.InvalidIdentifier
+ }
return Identifier{
- Kind: kind,
- Length: length,
- Value: id,
+ Kind: StringId,
Review Comment:
In rust sdk , they use as_code to map the enum to its actual code.
```rust
pub fn as_code(&self) -> u8 {
match self {
IdKind::Numeric => 1,
IdKind::String => 2,
}
}
```
However, Go doesn't have real enums; instead, we typically use constants. So
why not use it like constant.
```golang
type IdKind int // I will change it to uint8 later, might be better for
understanding.
const (
NumericId IdKind = 1
StringId IdKind = 2
)
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]