Skip to content

Commit bb29630

Browse files
authored
Merge pull request #3 from anc95/develop
👏 MVP VERSION DONE
2 parents 6fd0090 + ffed369 commit bb29630

File tree

6 files changed

+197
-3
lines changed

6 files changed

+197
-3
lines changed

README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,46 @@
11
# golang-enum-to-ts
22
Transform Golang `enum` type to Typescript enum
3+
4+
## Function
5+
6+
### Before (Golang)
7+
```golang
8+
package some
9+
10+
type Status int
11+
12+
const (
13+
Todo Status = iota
14+
Done
15+
Pending
16+
InProgress
17+
)
18+
19+
type Sex string
20+
21+
const (
22+
Female Sex = "female"
23+
Male Sex = "male"
24+
)
25+
26+
func Abctext() {
27+
//dadsad
28+
}
29+
```
30+
31+
### After (Typescript)
32+
33+
```ts
34+
namespace some {
35+
export enum Sex {
36+
Female = 'female',
37+
Male = 'male',
38+
}
39+
export enum Status {
40+
Pending = 2,
41+
InProgress = 3,
42+
Todo = 0,
43+
Done = 1,
44+
}
45+
}
46+
```

src/ast/__snapshots__/ast_test.snap

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@
33
ast.File{
44
Name: "some",
55
Body: {
6-
ast.TypeDeclaration{Id:"Status", Kind:"string"},
6+
ast.TypeDeclaration{Id:"Status", Kind:"int"},
77
ast.ConstDeclaration{
88
Declarators: {
99
{Kind:"Status", Id:"Todo", Value:"iota"},
1010
{Kind:"", Id:"Done", Value:""},
11+
{Kind:"", Id:"Pending", Value:""},
1112
{Kind:"", Id:"InProgress", Value:""},
1213
},
1314
},
14-
ast.TypeDeclaration{Id:"Sex", Kind:"int"},
15+
ast.TypeDeclaration{Id:"Sex", Kind:"string"},
1516
ast.ConstDeclaration{
1617
Declarators: {
1718
{Kind:"Sex", Id:"Female", Value:"female"},

src/ast/ast.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func (a *AstGenerator) readTypeDeclaration() TypeDeclaration {
8383

8484
next, _ = a.nextToken(true)
8585

86-
if next.Type != token.IntType {
86+
if next.Type == token.IntType {
8787
d.Kind = Int
8888
} else {
8989
d.Kind = String
@@ -134,6 +134,8 @@ func (a *AstGenerator) readConstDeclaration() ConstDeclaration {
134134
if a.currentToken.Type == token.RightParentheses {
135135
break
136136
}
137+
138+
a.backToken()
137139
}
138140

139141
a.matchNextLine()
@@ -188,3 +190,7 @@ func (a *AstGenerator) Gen() File {
188190

189191
return file
190192
}
193+
194+
func NewAstGenerator(tokens []token.Token) AstGenerator {
195+
return AstGenerator{Tokens: tokens, index: -1}
196+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
[TestNormal - 1]
3+
namespace some {
4+
export enum Status {
5+
Todo = 0,
6+
Done = 1,
7+
Pending = 2,
8+
InProgress = 3,
9+
}
10+
export enum Sex {
11+
Female = 'female',
12+
Male = 'male',
13+
}
14+
}
15+
16+
---

src/generator/generator.go

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,104 @@
11
package generator
2+
3+
import (
4+
"fmt"
5+
"strconv"
6+
7+
"github.com/anc95/golang-enum-to-ts/src/ast"
8+
)
9+
10+
func getEnum(f ast.File) map[string]map[string]interface{} {
11+
body := f.Body
12+
enumType := make(map[string]string)
13+
enumValue := make(map[string]map[string]interface{})
14+
15+
for i := 0; i < len(body); i++ {
16+
decl := body[i]
17+
18+
switch decl.(type) {
19+
case ast.TypeDeclaration:
20+
decl := decl.(ast.TypeDeclaration)
21+
enumType[decl.Id] = string(decl.Kind)
22+
case ast.ConstDeclaration:
23+
decl := decl.(ast.ConstDeclaration)
24+
kind := ""
25+
iotaValue := 0
26+
useIota := false
27+
iotaFlag := false
28+
var prevValue interface{}
29+
30+
for _, x := range decl.Declarators {
31+
if x.Kind != "" {
32+
kind = x.Kind
33+
34+
if enumValue[kind] == nil {
35+
enumValue[kind] = map[string]interface{}{}
36+
}
37+
}
38+
39+
if iotaFlag {
40+
iotaValue += 1
41+
}
42+
43+
if x.Value != "" {
44+
if x.Value == "iota" {
45+
useIota = true
46+
iotaFlag = true
47+
enumValue[kind][x.Id] = iotaValue
48+
} else {
49+
useIota = false
50+
51+
if kind == "int" || enumType[kind] == "int" {
52+
val, _ := strconv.Atoi(x.Value)
53+
enumValue[kind][x.Id] = val
54+
prevValue = val
55+
}
56+
57+
enumValue[kind][x.Id] = x.Value
58+
prevValue = x.Value
59+
}
60+
} else {
61+
if useIota {
62+
enumValue[kind][x.Id] = iotaValue
63+
} else {
64+
enumValue[kind][x.Id] = prevValue
65+
}
66+
}
67+
}
68+
}
69+
}
70+
71+
return enumValue
72+
}
73+
74+
func GenerateTS(f ast.File) string {
75+
enumValue := getEnum(f)
76+
result := ""
77+
78+
for key, val := range enumValue {
79+
ret := ""
80+
81+
for k, v := range val {
82+
if ret != "" {
83+
ret += "\n"
84+
}
85+
86+
switch v.(type) {
87+
case int:
88+
ret += fmt.Sprintf(" %s = %d,", k, v)
89+
case string:
90+
ret += fmt.Sprintf(" %s = '%s',", k, v)
91+
}
92+
}
93+
94+
ret = fmt.Sprintf(" export enum %s {\n", key) + ret
95+
ret += "\n }\n"
96+
97+
result += ret
98+
}
99+
100+
result = fmt.Sprintf("namespace %s {\n", f.Name) + result
101+
result += "}\n"
102+
103+
return result
104+
}

src/generator/generator_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package generator
2+
3+
import (
4+
"os"
5+
"path"
6+
"testing"
7+
8+
"github.com/anc95/golang-enum-to-ts/src/ast"
9+
"github.com/anc95/golang-enum-to-ts/src/token"
10+
"github.com/gkampitakis/go-snaps/snaps"
11+
)
12+
13+
func TestNormal(t *testing.T) {
14+
wd, _ := os.Getwd()
15+
16+
a := path.Join(wd, "../test-cases/normal.go")
17+
parser := token.NewParser(a)
18+
tokens := parser.Parse()
19+
ast := ast.NewAstGenerator(tokens)
20+
21+
result := GenerateTS(ast.Gen())
22+
23+
snaps.MatchSnapshot(t, result)
24+
}

0 commit comments

Comments
 (0)