fix: use const name as enum name

This commit is contained in:
franklin 2021-04-29 16:47:57 +02:00
parent 3d2c29f004
commit a7d5df1121
10 changed files with 72 additions and 74 deletions

1
.gitignore vendored
View File

@ -2,3 +2,4 @@
/vendor/
dist/
.idea
bin/

View File

@ -3,16 +3,23 @@ var GoTSRPC;
(function (GoTSRPC) {
var Demo;
(function (Demo) {
// github.com/foomo/gotsrpc/demo.CustomTypeInt
var CustomTypeInt;
(function (CustomTypeInt) {
CustomTypeInt[CustomTypeInt["One"] = 1] = "One";
CustomTypeInt[CustomTypeInt["Three"] = 3] = "Three";
CustomTypeInt[CustomTypeInt["Two"] = 2] = "Two";
})(CustomTypeInt = Demo.CustomTypeInt || (Demo.CustomTypeInt = {}));
// github.com/foomo/gotsrpc/demo.CustomTypeString
var CustomTypeString;
(function (CustomTypeString) {
CustomTypeString["Regular"] = "regular";
CustomTypeString["CamelCase"] = "camelCase";
CustomTypeString["SnakeCase"] = "snake_case";
CustomTypeString["SlugCase"] = "slug-case";
CustomTypeString["ConstCase"] = "CONST_CASE";
CustomTypeString["SlugCaseUpper"] = "SLUG-CASE-UPPER";
CustomTypeString["DotCase"] = "dot.case";
CustomTypeString["Five"] = "CONST_CASE";
CustomTypeString["Four"] = "slug-case";
CustomTypeString["One"] = "regular";
CustomTypeString["Seven"] = "dot.case";
CustomTypeString["Six"] = "SLUG-CASE-UPPER";
CustomTypeString["Three"] = "snake_case";
CustomTypeString["Two"] = "camelCase";
})(CustomTypeString = Demo.CustomTypeString || (Demo.CustomTypeString = {}));
})(Demo = GoTSRPC.Demo || (GoTSRPC.Demo = {}));
})(GoTSRPC || (GoTSRPC = {}));
@ -27,8 +34,8 @@ var GoTSRPC;
var CustomTypeNested;
(function (CustomTypeNested) {
CustomTypeNested["One"] = "one";
CustomTypeNested["Two"] = "two";
CustomTypeNested["Three"] = "three";
CustomTypeNested["Two"] = "two";
})(CustomTypeNested = Nested.CustomTypeNested || (Nested.CustomTypeNested = {}));
})(Nested = Demo.Nested || (Demo.Nested = {}));
})(Demo = GoTSRPC.Demo || (GoTSRPC.Demo = {}));

View File

@ -9,8 +9,8 @@ export type Any = any
// github.com/foomo/gotsrpc/demo/nested.CustomTypeNested
export enum CustomTypeNested {
One = "one",
Two = "two",
Three = "three",
Two = "two",
}
// github.com/foomo/gotsrpc/demo/nested.Nested
export interface Nested {

View File

@ -16,8 +16,6 @@ export interface Address {
}
// github.com/foomo/gotsrpc/demo.Bar
export type Bar = any
// github.com/foomo/gotsrpc/demo.BarGoTSRPCClient
export type BarGoTSRPCClient = any
// github.com/foomo/gotsrpc/demo.Check
export interface Check {
Foo:string;
@ -25,16 +23,20 @@ export interface Check {
// github.com/foomo/gotsrpc/demo.CustomTypeFoo
export type CustomTypeFoo = string
// github.com/foomo/gotsrpc/demo.CustomTypeInt
export type CustomTypeInt = 1 | 2 | 3
export enum CustomTypeInt {
One = 1,
Three = 3,
Two = 2,
}
// github.com/foomo/gotsrpc/demo.CustomTypeString
export enum CustomTypeString {
Regular = "regular",
CamelCase = "camelCase",
SnakeCase = "snake_case",
SlugCase = "slug-case",
ConstCase = "CONST_CASE",
SlugCaseUpper = "SLUG-CASE-UPPER",
DotCase = "dot.case",
Five = "CONST_CASE",
Four = "slug-case",
One = "regular",
Seven = "dot.case",
Six = "SLUG-CASE-UPPER",
Three = "snake_case",
Two = "camelCase",
}
// github.com/foomo/gotsrpc/demo.CustomTypeStruct
export interface CustomTypeStruct {
@ -44,14 +46,10 @@ export interface CustomTypeStruct {
CustomTypeNested:github_com_foomo_gotsrpc_demo_nested.CustomTypeNested;
Check:github_com_foomo_gotsrpc_demo.Check;
}
// github.com/foomo/gotsrpc/demo.DemoGoTSRPCClient
export type DemoGoTSRPCClient = any
// github.com/foomo/gotsrpc/demo.Err
export interface Err {
message:string;
}
// github.com/foomo/gotsrpc/demo.FooGoTSRPCClient
export type FooGoTSRPCClient = any
// github.com/foomo/gotsrpc/demo.Inner
export interface Inner {
one:string;

View File

@ -9,8 +9,8 @@ export type Any = any
// github.com/foomo/gotsrpc/demo/nested.CustomTypeNested
export enum CustomTypeNested {
One = "one",
Two = "two",
Three = "three",
Two = "two",
}
// github.com/foomo/gotsrpc/demo/nested.Nested
export interface Nested {

View File

@ -16,8 +16,6 @@ export interface Address {
}
// github.com/foomo/gotsrpc/demo.Bar
export type Bar = any
// github.com/foomo/gotsrpc/demo.BarGoTSRPCClient
export type BarGoTSRPCClient = any
// github.com/foomo/gotsrpc/demo.Check
export interface Check {
Foo:string;
@ -25,16 +23,20 @@ export interface Check {
// github.com/foomo/gotsrpc/demo.CustomTypeFoo
export type CustomTypeFoo = string
// github.com/foomo/gotsrpc/demo.CustomTypeInt
export type CustomTypeInt = 1 | 2 | 3
export enum CustomTypeInt {
One = 1,
Three = 3,
Two = 2,
}
// github.com/foomo/gotsrpc/demo.CustomTypeString
export enum CustomTypeString {
Regular = "regular",
CamelCase = "camelCase",
SnakeCase = "snake_case",
SlugCase = "slug-case",
ConstCase = "CONST_CASE",
SlugCaseUpper = "SLUG-CASE-UPPER",
DotCase = "dot.case",
Five = "CONST_CASE",
Four = "slug-case",
One = "regular",
Seven = "dot.case",
Six = "SLUG-CASE-UPPER",
Three = "snake_case",
Two = "camelCase",
}
// github.com/foomo/gotsrpc/demo.CustomTypeStruct
export interface CustomTypeStruct {
@ -44,14 +46,10 @@ export interface CustomTypeStruct {
CustomTypeNested:github_com_foomo_gotsrpc_demo_nested.CustomTypeNested;
Check:github_com_foomo_gotsrpc_demo.Check;
}
// github.com/foomo/gotsrpc/demo.DemoGoTSRPCClient
export type DemoGoTSRPCClient = any
// github.com/foomo/gotsrpc/demo.Err
export interface Err {
message:string;
}
// github.com/foomo/gotsrpc/demo.FooGoTSRPCClient
export type FooGoTSRPCClient = any
// github.com/foomo/gotsrpc/demo.Inner
export interface Inner {
one:string;

View File

@ -7,8 +7,8 @@ module GoTSRPC.Demo.Nested {
// github.com/foomo/gotsrpc/demo/nested.CustomTypeNested
export enum CustomTypeNested {
One = "one",
Two = "two",
Three = "three",
Two = "two",
}
// github.com/foomo/gotsrpc/demo/nested.Nested
export interface Nested {

View File

@ -14,8 +14,6 @@ module GoTSRPC.Demo {
}
// github.com/foomo/gotsrpc/demo.Bar
export type Bar = any
// github.com/foomo/gotsrpc/demo.BarGoTSRPCClient
export type BarGoTSRPCClient = any
// github.com/foomo/gotsrpc/demo.Check
export interface Check {
Foo:string;
@ -23,16 +21,20 @@ module GoTSRPC.Demo {
// github.com/foomo/gotsrpc/demo.CustomTypeFoo
export type CustomTypeFoo = string
// github.com/foomo/gotsrpc/demo.CustomTypeInt
export type CustomTypeInt = 1 | 2 | 3
export enum CustomTypeInt {
One = 1,
Three = 3,
Two = 2,
}
// github.com/foomo/gotsrpc/demo.CustomTypeString
export enum CustomTypeString {
Regular = "regular",
CamelCase = "camelCase",
SnakeCase = "snake_case",
SlugCase = "slug-case",
ConstCase = "CONST_CASE",
SlugCaseUpper = "SLUG-CASE-UPPER",
DotCase = "dot.case",
Five = "CONST_CASE",
Four = "slug-case",
One = "regular",
Seven = "dot.case",
Six = "SLUG-CASE-UPPER",
Three = "snake_case",
Two = "camelCase",
}
// github.com/foomo/gotsrpc/demo.CustomTypeStruct
export interface CustomTypeStruct {
@ -42,14 +44,10 @@ module GoTSRPC.Demo {
CustomTypeNested:GoTSRPC.Demo.Nested.CustomTypeNested;
Check:GoTSRPC.Demo.Check;
}
// github.com/foomo/gotsrpc/demo.DemoGoTSRPCClient
export type DemoGoTSRPCClient = any
// github.com/foomo/gotsrpc/demo.Err
export interface Err {
message:string;
}
// github.com/foomo/gotsrpc/demo.FooGoTSRPCClient
export type FooGoTSRPCClient = any
// github.com/foomo/gotsrpc/demo.Inner
export interface Inner {
one:string;

View File

@ -240,14 +240,12 @@ func loadConstantTypes(pkg *ast.Package) map[string]interface{} {
if specType, ok := spec.Type.(*ast.Ident); ok {
for _, val := range spec.Values {
if reflect.ValueOf(val).Type().String() == "*ast.BasicLit" {
firstValueLit := val.(*ast.BasicLit)
var values []*ast.BasicLit
if value, ok := constantTypes[specType.Name]; ok {
if v, ok := value.([]*ast.BasicLit); ok {
values = v
}
if _, ok := constantTypes[specType.Name]; !ok {
constantTypes[specType.Name] = map[string]*ast.BasicLit{}
} else if _, ok := constantTypes[specType.Name].(map[string]*ast.BasicLit); !ok {
constantTypes[specType.Name] = map[string]*ast.BasicLit{}
}
constantTypes[specType.Name] = append(values, firstValueLit)
constantTypes[specType.Name].(map[string]*ast.BasicLit)[spec.Names[0].Name] = val.(*ast.BasicLit)
}
}
}

View File

@ -3,10 +3,11 @@ package gotsrpc
import (
"errors"
"go/ast"
"sort"
"strings"
"github.com/foomo/gotsrpc/config"
"github.com/iancoleman/strcase"
"github.com/iancoleman/strcase"
)
// @todo refactor this is wrong
@ -186,22 +187,19 @@ func renderTypescriptStructsToPackages(
}
packageCodeMap[packageConstantTypeName].l("// " + packageName + "." + packageConstantTypeName)
if packageConstantTypeValuesList, ok := packageConstantTypeValues.([]*ast.BasicLit); ok {
if strings.HasPrefix(packageConstantTypeValuesList[0].Value, "\"") {
if packageConstantTypeValuesList, ok := packageConstantTypeValues.(map[string]*ast.BasicLit); ok {
keys := make([]string, 0, len(packageConstantTypeValuesList))
for k := range packageConstantTypeValuesList {
keys = append(keys, k)
}
sort.Strings(keys)
packageCodeMap[packageConstantTypeName].l("export enum " + packageConstantTypeName + " {").ind(1)
for _, packageConstantTypeValue := range packageConstantTypeValuesList {
enum := strings.Replace(packageConstantTypeValue.Value, "\"", "", -1)
enum = strcase.ToCamel(strcase.ToSnake(enum))
packageCodeMap[packageConstantTypeName].l(enum + " = " + packageConstantTypeValue.Value + ",")
for _, k := range keys {
enum := strings.TrimPrefix(strcase.ToCamel(k), packageConstantTypeName)
packageCodeMap[packageConstantTypeName].l(enum + " = " + packageConstantTypeValuesList[k].Value + ",")
}
packageCodeMap[packageConstantTypeName].ind(-1).l("}")
} else {
var values []string
for _, packageConstantTypeValue := range packageConstantTypeValuesList {
values = append(values, packageConstantTypeValue.Value)
}
packageCodeMap[packageConstantTypeName].l("export type " + packageConstantTypeName + " = " + strings.Join(values, " | "))
}
} else if packageConstantTypeValuesString, ok := packageConstantTypeValues.(string); ok {
packageCodeMap[packageConstantTypeName].l("export type " + packageConstantTypeName + " = " + packageConstantTypeValuesString)
}