Skip to content

Commit

Permalink
Replace generic gospeak.Enum[int] with enum.Int64, enum.Uint etc.
Browse files Browse the repository at this point in the history
The generic Enum type didn't work, as it failed with:
"cannot use a type parameter as RHS in type declaration"

golang/go#45639
  • Loading branch information
VojtechVitek committed Aug 3, 2023
1 parent 0dc2359 commit 93bb9aa
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 24 deletions.
7 changes: 0 additions & 7 deletions enum.go

This file was deleted.

29 changes: 29 additions & 0 deletions enum/enum.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package enum

// type Enum[T EnumType] T
//
// type EnumType interface {
// ~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~string
// }
//
// NOTE: The generic Enum type didn't work, as it failed with:
// "cannot use a type parameter as RHS in type declaration"
// https://github.com/golang/go/issues/45639

type Int int
type Uint uint

type Int8 int8
type Uint8 uint8

type Int16 int16
type Uint16 uint16

type Int32 int32
type Uint32 uint32

type Int64 int64
type Uint64 uint64

// webrpc TODO: string ENUM
// https://github.com/webrpc/webrpc/issues/203
31 changes: 17 additions & 14 deletions internal/parser/enums.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (p *Parser) CollectEnums() error {
if typeDeclaration, ok := decl.(*ast.GenDecl); ok && typeDeclaration.Tok == token.IMPORT {
for _, spec := range typeDeclaration.Specs {
if importSpec, ok := spec.(*ast.ImportSpec); ok {
if strings.Contains(importSpec.Path.Value, `"github.com/golang-cz/gospeak"`) {
if strings.Contains(importSpec.Path.Value, `"github.com/golang-cz/gospeak/enum"`) {
gospeakImportFound = true
}
}
Expand All @@ -49,23 +49,26 @@ func (p *Parser) CollectEnums() error {
if typeDeclaration, ok := decl.(*ast.GenDecl); ok && typeDeclaration.Tok == token.TYPE {
for _, spec := range typeDeclaration.Specs {
if typeSpec, ok := spec.(*ast.TypeSpec); ok {
var enumName, enumTypeName string
if iExpr, ok := typeSpec.Type.(*ast.IndexExpr); ok {
if selExpr, ok := iExpr.X.(*ast.SelectorExpr); ok {
pkgName, ok := selExpr.X.(*ast.Ident)
if ok && pkgName.Name == "gospeak" && selExpr.Sel.Name == "Enum" {
if id, ok := iExpr.Index.(*ast.Ident); ok {
enumName = typeSpec.Name.Name
enumTypeName = id.Name
}
}
}
//panic(debug.Sdump(typeSpec))

selExpr, ok := typeSpec.Type.(*ast.SelectorExpr)
if !ok {
continue
}
if enumName == "" || enumTypeName == "" {
ident, ok := selExpr.X.(*ast.Ident)
if !ok {
continue
}

// type Status enum.Int64
enumName := typeSpec.Name.Name // Status
pkgName := ident.Name // enum
enumTypeName := selExpr.Sel.Name // Int64
if pkgName != "enum" || enumName == "" || enumTypeName == "" {
continue
}

enumElemType, ok := schema.CoreTypeFromString[enumTypeName]
enumElemType, ok := schema.CoreTypeFromString[strings.ToLower(enumTypeName)]
if !ok {
return fmt.Errorf("unknown enum type %v", enumTypeName)
}
Expand Down
6 changes: 3 additions & 3 deletions internal/parser/test/enum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestStructFieldEnum(t *testing.T) {
// pending = 1
// closed = 2
// new = 3
type Status gospeak.Enum[int]
type Status enum.Int
`,
t: schema.T_Int,
out: []*schema.TypeField{
Expand All @@ -39,7 +39,7 @@ func TestStructFieldEnum(t *testing.T) {
// pending
// closed
// new
type Status gospeak.Enum[uint64]
type Status enum.Uint64
`,
t: schema.T_Uint64,
out: []*schema.TypeField{
Expand All @@ -57,7 +57,7 @@ func TestStructFieldEnum(t *testing.T) {
import (
"context"
"github.com/golang-cz/gospeak"
"github.com/golang-cz/gospeak/enum"
)
%s
Expand Down

0 comments on commit 93bb9aa

Please sign in to comment.