Skip to content

Commit 93bb9aa

Browse files
committed
Replace generic gospeak.Enum[int] with enum.Int64, enum.Uint etc.
The generic Enum type didn't work, as it failed with: "cannot use a type parameter as RHS in type declaration" golang/go#45639
1 parent 0dc2359 commit 93bb9aa

File tree

4 files changed

+49
-24
lines changed

4 files changed

+49
-24
lines changed

enum.go

Lines changed: 0 additions & 7 deletions
This file was deleted.

enum/enum.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package enum
2+
3+
// type Enum[T EnumType] T
4+
//
5+
// type EnumType interface {
6+
// ~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~string
7+
// }
8+
//
9+
// NOTE: The generic Enum type didn't work, as it failed with:
10+
// "cannot use a type parameter as RHS in type declaration"
11+
// https://github.com/golang/go/issues/45639
12+
13+
type Int int
14+
type Uint uint
15+
16+
type Int8 int8
17+
type Uint8 uint8
18+
19+
type Int16 int16
20+
type Uint16 uint16
21+
22+
type Int32 int32
23+
type Uint32 uint32
24+
25+
type Int64 int64
26+
type Uint64 uint64
27+
28+
// webrpc TODO: string ENUM
29+
// https://github.com/webrpc/webrpc/issues/203

internal/parser/enums.go

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func (p *Parser) CollectEnums() error {
3232
if typeDeclaration, ok := decl.(*ast.GenDecl); ok && typeDeclaration.Tok == token.IMPORT {
3333
for _, spec := range typeDeclaration.Specs {
3434
if importSpec, ok := spec.(*ast.ImportSpec); ok {
35-
if strings.Contains(importSpec.Path.Value, `"github.com/golang-cz/gospeak"`) {
35+
if strings.Contains(importSpec.Path.Value, `"github.com/golang-cz/gospeak/enum"`) {
3636
gospeakImportFound = true
3737
}
3838
}
@@ -49,23 +49,26 @@ func (p *Parser) CollectEnums() error {
4949
if typeDeclaration, ok := decl.(*ast.GenDecl); ok && typeDeclaration.Tok == token.TYPE {
5050
for _, spec := range typeDeclaration.Specs {
5151
if typeSpec, ok := spec.(*ast.TypeSpec); ok {
52-
var enumName, enumTypeName string
53-
if iExpr, ok := typeSpec.Type.(*ast.IndexExpr); ok {
54-
if selExpr, ok := iExpr.X.(*ast.SelectorExpr); ok {
55-
pkgName, ok := selExpr.X.(*ast.Ident)
56-
if ok && pkgName.Name == "gospeak" && selExpr.Sel.Name == "Enum" {
57-
if id, ok := iExpr.Index.(*ast.Ident); ok {
58-
enumName = typeSpec.Name.Name
59-
enumTypeName = id.Name
60-
}
61-
}
62-
}
52+
//panic(debug.Sdump(typeSpec))
53+
54+
selExpr, ok := typeSpec.Type.(*ast.SelectorExpr)
55+
if !ok {
56+
continue
6357
}
64-
if enumName == "" || enumTypeName == "" {
58+
ident, ok := selExpr.X.(*ast.Ident)
59+
if !ok {
60+
continue
61+
}
62+
63+
// type Status enum.Int64
64+
enumName := typeSpec.Name.Name // Status
65+
pkgName := ident.Name // enum
66+
enumTypeName := selExpr.Sel.Name // Int64
67+
if pkgName != "enum" || enumName == "" || enumTypeName == "" {
6568
continue
6669
}
6770

68-
enumElemType, ok := schema.CoreTypeFromString[enumTypeName]
71+
enumElemType, ok := schema.CoreTypeFromString[strings.ToLower(enumTypeName)]
6972
if !ok {
7073
return fmt.Errorf("unknown enum type %v", enumTypeName)
7174
}

internal/parser/test/enum_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func TestStructFieldEnum(t *testing.T) {
2222
// pending = 1
2323
// closed = 2
2424
// new = 3
25-
type Status gospeak.Enum[int]
25+
type Status enum.Int
2626
`,
2727
t: schema.T_Int,
2828
out: []*schema.TypeField{
@@ -39,7 +39,7 @@ func TestStructFieldEnum(t *testing.T) {
3939
// pending
4040
// closed
4141
// new
42-
type Status gospeak.Enum[uint64]
42+
type Status enum.Uint64
4343
`,
4444
t: schema.T_Uint64,
4545
out: []*schema.TypeField{
@@ -57,7 +57,7 @@ func TestStructFieldEnum(t *testing.T) {
5757
import (
5858
"context"
5959
60-
"github.com/golang-cz/gospeak"
60+
"github.com/golang-cz/gospeak/enum"
6161
)
6262
6363
%s

0 commit comments

Comments
 (0)