feat: remove GoConst

This commit is contained in:
franklin 2021-04-29 15:01:44 +02:00
parent befd147899
commit e94566a540
10 changed files with 69 additions and 171 deletions

View File

@ -112,7 +112,7 @@ func Build(conf *config.Config, goPath string) {
goPaths = append(goPaths, vendorDirectory)
}
pkgName, services, structs, scalars, constants, constantTypes, err := Read(goPaths, conf.Module, packageName, target.Services)
pkgName, services, structs, scalars, constantTypes, err := Read(goPaths, conf.Module, packageName, target.Services)
if err != nil {
fmt.Fprintln(os.Stderr, "\t an error occured while trying to understand your code: ", err)
@ -140,7 +140,7 @@ func Build(conf *config.Config, goPath string) {
os.Exit(3)
}
err = renderTypescriptStructsToPackages(conf.ModuleKind, structs, conf.Mappings, constants, constantTypes, scalars, mappedTypeScript)
err = renderTypescriptStructsToPackages(conf.ModuleKind, structs, conf.Mappings, constantTypes, scalars, mappedTypeScript)
if err != nil {
fmt.Fprintln(os.Stderr, "struct gen err for target", name, err)
os.Exit(4)
@ -223,7 +223,7 @@ func Build(conf *config.Config, goPath string) {
moduleCode.l("module " + mapping.TypeScriptModule + " {").ind(1)
}
structNames := []string{"___goConstants"}
var structNames []string
for structName := range mappedStructsMap {
structNames = append(structNames, structName)

View File

@ -3,15 +3,13 @@ var GoTSRPC;
(function (GoTSRPC) {
var Demo;
(function (Demo) {
// constants from github.com/foomo/gotsrpc/demo
Demo.GoConst = {
CustomTypeIntOne: 1,
CustomTypeIntThree: 3,
CustomTypeIntTwo: 2,
CustomTypeStringOne: "one",
CustomTypeStringThree: "three",
CustomTypeStringTwo: "two"
};
// github.com/foomo/gotsrpc/demo.CustomTypeString
var CustomTypeString;
(function (CustomTypeString) {
CustomTypeString["one"] = "one";
CustomTypeString["two"] = "two";
CustomTypeString["three"] = "three";
})(CustomTypeString = Demo.CustomTypeString || (Demo.CustomTypeString = {}));
})(Demo = GoTSRPC.Demo || (GoTSRPC.Demo = {}));
})(GoTSRPC || (GoTSRPC = {}));
/* eslint:disable */
@ -21,12 +19,13 @@ var GoTSRPC;
(function (Demo) {
var Nested;
(function (Nested) {
// constants from github.com/foomo/gotsrpc/demo/nested
Nested.GoConst = {
CustomTypeNestedOne: "one",
CustomTypeNestedThree: "three",
CustomTypeNestedTwo: "two"
};
// github.com/foomo/gotsrpc/demo/nested.CustomTypeNested
var CustomTypeNested;
(function (CustomTypeNested) {
CustomTypeNested["one"] = "one";
CustomTypeNested["two"] = "two";
CustomTypeNested["three"] = "three";
})(CustomTypeNested = Nested.CustomTypeNested || (Nested.CustomTypeNested = {}));
})(Nested = Demo.Nested || (Demo.Nested = {}));
})(Demo = GoTSRPC.Demo || (GoTSRPC.Demo = {}));
})(GoTSRPC || (GoTSRPC = {}));

View File

@ -7,7 +7,11 @@ export type Amount = number
// github.com/foomo/gotsrpc/demo/nested.Any
export type Any = any
// github.com/foomo/gotsrpc/demo/nested.CustomTypeNested
export type CustomTypeNested = "one" | "two" | "three"
export enum CustomTypeNested {
one = "one",
two = "two",
three = "three",
}
// github.com/foomo/gotsrpc/demo/nested.Nested
export interface Nested {
Name:string;
@ -23,10 +27,4 @@ export interface Nested {
}
// github.com/foomo/gotsrpc/demo/nested.True
export type True = boolean
// constants from github.com/foomo/gotsrpc/demo/nested
export const GoConst = {
CustomTypeNestedOne : "one",
CustomTypeNestedThree : "three",
CustomTypeNestedTwo : "two",
}
// end of common js

View File

@ -25,7 +25,11 @@ export type CustomTypeFoo = string
// github.com/foomo/gotsrpc/demo.CustomTypeInt
export type CustomTypeInt = 1 | 2 | 3
// github.com/foomo/gotsrpc/demo.CustomTypeString
export type CustomTypeString = "one" | "two" | "three"
export enum CustomTypeString {
one = "one",
two = "two",
three = "three",
}
// github.com/foomo/gotsrpc/demo.CustomTypeStruct
export interface CustomTypeStruct {
CustomTypeFoo:github_com_foomo_gotsrpc_demo.CustomTypeFoo;
@ -56,13 +60,4 @@ export interface Person {
export type ScalarError = string
// github.com/foomo/gotsrpc/demo.ScalarInPlace
export type ScalarInPlace = string
// constants from github.com/foomo/gotsrpc/demo
export const GoConst = {
CustomTypeIntOne : 1,
CustomTypeIntThree : 3,
CustomTypeIntTwo : 2,
CustomTypeStringOne : "one",
CustomTypeStringThree : "three",
CustomTypeStringTwo : "two",
}
// end of common js

View File

@ -7,7 +7,11 @@ export type Amount = number
// github.com/foomo/gotsrpc/demo/nested.Any
export type Any = any
// github.com/foomo/gotsrpc/demo/nested.CustomTypeNested
export type CustomTypeNested = "one" | "two" | "three"
export enum CustomTypeNested {
one = "one",
two = "two",
three = "three",
}
// github.com/foomo/gotsrpc/demo/nested.Nested
export interface Nested {
Name:string;
@ -23,10 +27,4 @@ export interface Nested {
}
// github.com/foomo/gotsrpc/demo/nested.True
export type True = boolean
// constants from github.com/foomo/gotsrpc/demo/nested
export const GoConst = {
CustomTypeNestedOne : "one",
CustomTypeNestedThree : "three",
CustomTypeNestedTwo : "two",
}
// end of common js

View File

@ -25,7 +25,11 @@ export type CustomTypeFoo = string
// github.com/foomo/gotsrpc/demo.CustomTypeInt
export type CustomTypeInt = 1 | 2 | 3
// github.com/foomo/gotsrpc/demo.CustomTypeString
export type CustomTypeString = "one" | "two" | "three"
export enum CustomTypeString {
one = "one",
two = "two",
three = "three",
}
// github.com/foomo/gotsrpc/demo.CustomTypeStruct
export interface CustomTypeStruct {
CustomTypeFoo:github_com_foomo_gotsrpc_demo.CustomTypeFoo;
@ -56,13 +60,4 @@ export interface Person {
export type ScalarError = string
// github.com/foomo/gotsrpc/demo.ScalarInPlace
export type ScalarInPlace = string
// constants from github.com/foomo/gotsrpc/demo
export const GoConst = {
CustomTypeIntOne : 1,
CustomTypeIntThree : 3,
CustomTypeIntTwo : 2,
CustomTypeStringOne : "one",
CustomTypeStringThree : "three",
CustomTypeStringTwo : "two",
}
// end of common js

View File

@ -5,7 +5,11 @@ module GoTSRPC.Demo.Nested {
// github.com/foomo/gotsrpc/demo/nested.Any
export type Any = any
// github.com/foomo/gotsrpc/demo/nested.CustomTypeNested
export type CustomTypeNested = "one" | "two" | "three"
export enum CustomTypeNested {
one = "one",
two = "two",
three = "three",
}
// github.com/foomo/gotsrpc/demo/nested.Nested
export interface Nested {
Name:string;
@ -21,10 +25,4 @@ module GoTSRPC.Demo.Nested {
}
// github.com/foomo/gotsrpc/demo/nested.True
export type True = boolean
// constants from github.com/foomo/gotsrpc/demo/nested
export const GoConst = {
CustomTypeNestedOne : "one",
CustomTypeNestedThree : "three",
CustomTypeNestedTwo : "two",
}
}

View File

@ -23,7 +23,11 @@ module GoTSRPC.Demo {
// github.com/foomo/gotsrpc/demo.CustomTypeInt
export type CustomTypeInt = 1 | 2 | 3
// github.com/foomo/gotsrpc/demo.CustomTypeString
export type CustomTypeString = "one" | "two" | "three"
export enum CustomTypeString {
one = "one",
two = "two",
three = "three",
}
// github.com/foomo/gotsrpc/demo.CustomTypeStruct
export interface CustomTypeStruct {
CustomTypeFoo:GoTSRPC.Demo.CustomTypeFoo;
@ -54,13 +58,4 @@ module GoTSRPC.Demo {
export type ScalarError = string
// github.com/foomo/gotsrpc/demo.ScalarInPlace
export type ScalarInPlace = string
// constants from github.com/foomo/gotsrpc/demo
export const GoConst = {
CustomTypeIntOne : 1,
CustomTypeIntThree : 3,
CustomTypeIntTwo : 2,
CustomTypeStringOne : "one",
CustomTypeStringThree : "three",
CustomTypeStringTwo : "two",
}
}

View File

@ -193,42 +193,8 @@ func readServicesInPackage(pkg *ast.Package, packageName string, serviceMap map[
return
}
func loadConstants(pkg *ast.Package) map[string]*ast.BasicLit {
constants := map[string]*ast.BasicLit{}
for _, file := range pkg.Files {
for _, decl := range file.Decls {
if reflect.ValueOf(decl).Type().String() == "*ast.GenDecl" {
genDecl := decl.(*ast.GenDecl)
if genDecl.Tok == token.CONST {
trace("got a const", genDecl.Specs)
for _, spec := range genDecl.Specs {
if "*ast.ValueSpec" == reflect.ValueOf(spec).Type().String() {
spec := spec.(*ast.ValueSpec)
for _, val := range spec.Values {
if reflect.ValueOf(val).Type().String() == "*ast.BasicLit" {
firstValueLit := val.(*ast.BasicLit)
constName := spec.Names[0].String()
for indexRune, r := range constName {
if indexRune == 0 {
if string(r) == strings.ToUpper(string(r)) {
constants[constName] = firstValueLit
}
break
}
}
}
}
}
}
}
}
}
}
return constants
}
func loadConstantTypes(pkg *ast.Package) map[string]interface{} {
constants := map[string]interface{}{}
constantTypes := map[string]interface{}{}
for _, file := range pkg.Files {
for _, decl := range file.Decls {
if reflect.ValueOf(decl).Type().String() == "*ast.GenDecl" {
@ -239,25 +205,25 @@ func loadConstantTypes(pkg *ast.Package) map[string]interface{} {
for _, spec := range genDecl.Specs {
if reflect.ValueOf(spec).Type().String() == "*ast.TypeSpec" {
spec := spec.(*ast.TypeSpec)
if _, ok := constants[spec.Name.Name]; ok {
if _, ok := constantTypes[spec.Name.Name]; ok {
continue
}
switch reflect.ValueOf(spec.Type).Type().String() {
case "*ast.InterfaceType":
constants[spec.Name.Name] = "any"
constantTypes[spec.Name.Name] = "any"
case "*ast.Ident":
specIdent := spec.Type.(*ast.Ident)
switch specIdent.Name {
case "byte":
constants[spec.Name.Name] = "any"
constantTypes[spec.Name.Name] = "any"
case "string":
constants[spec.Name.Name] = "string"
constantTypes[spec.Name.Name] = "string"
case "bool":
constants[spec.Name.Name] = "boolean"
constantTypes[spec.Name.Name] = "boolean"
case "float", "float32", "float64",
"int", "int8", "int16", "int32", "int64",
"uint", "uint8", "uint16", "uint32", "uint64":
constants[spec.Name.Name] = "number"
constantTypes[spec.Name.Name] = "number"
default:
trace("unhandled type", reflect.ValueOf(spec.Type).Type().String())
}
@ -276,12 +242,12 @@ func loadConstantTypes(pkg *ast.Package) map[string]interface{} {
if reflect.ValueOf(val).Type().String() == "*ast.BasicLit" {
firstValueLit := val.(*ast.BasicLit)
var values []*ast.BasicLit
if value, ok := constants[specType.Name]; ok {
if value, ok := constantTypes[specType.Name]; ok {
if v, ok := value.([]*ast.BasicLit); ok {
values = v
}
}
constants[specType.Name] = append(values, firstValueLit)
constantTypes[specType.Name] = append(values, firstValueLit)
}
}
}
@ -291,7 +257,7 @@ func loadConstantTypes(pkg *ast.Package) map[string]interface{} {
}
}
}
return constants
return constantTypes
}
@ -305,7 +271,6 @@ func Read(
services ServiceList,
structs map[string]*Struct,
scalars map[string]*Scalar,
constants map[string]map[string]*ast.BasicLit,
constantTypes map[string]map[string]interface{},
err error,
) {
@ -349,22 +314,6 @@ func Read(
trace("---------------- found scalars -------------------")
traceData(scalars)
trace("---------------- /found scalars -------------------")
constants = map[string]map[string]*ast.BasicLit{}
for _, structDef := range structs {
if structDef != nil {
structPackage := structDef.Package
_, ok := constants[structPackage]
if !ok {
// fmt.Println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", structPackage)
pkg, constPkgErr := parsePackage(goPaths, gomod, structPackage)
if constPkgErr != nil {
err = constPkgErr
return
}
constants[structPackage] = loadConstants(pkg)
}
}
}
constantTypes = map[string]map[string]interface{}{}
for _, structDef := range structs {
if structDef != nil {

View File

@ -2,16 +2,12 @@ package gotsrpc
import (
"errors"
"fmt"
"go/ast"
"sort"
"strings"
"github.com/foomo/gotsrpc/config"
)
const goConstPseudoPackage = "__goConstants"
// @todo refactor this is wrong
var SkipGoTSRPC = false
@ -146,7 +142,6 @@ func renderTypescriptStructsToPackages(
moduleKind config.ModuleKind,
structs map[string]*Struct,
mappings config.TypeScriptMappings,
constants map[string]map[string]*ast.BasicLit,
constantTypes map[string]map[string]interface{},
scalarTypes map[string]*Scalar,
mappedTypeScript map[string]map[string]*code,
@ -191,11 +186,19 @@ func renderTypescriptStructsToPackages(
packageCodeMap[packageConstantTypeName].l("// " + packageName + "." + packageConstantTypeName)
if packageConstantTypeValuesList, ok := packageConstantTypeValues.([]*ast.BasicLit); ok {
var values []string
for _, packageConstantTypeValue := range packageConstantTypeValuesList {
values = append(values, packageConstantTypeValue.Value)
if strings.HasPrefix(packageConstantTypeValuesList[0].Value, "\"") {
packageCodeMap[packageConstantTypeName].l("export enum " + packageConstantTypeName + " {").ind(1)
for _, packageConstantTypeValue := range packageConstantTypeValuesList {
packageCodeMap[packageConstantTypeName].l(strings.Replace(packageConstantTypeValue.Value, "\"", "", -1) + " = " + packageConstantTypeValue.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, " | "))
}
packageCodeMap[packageConstantTypeName].l("export type " + packageConstantTypeName + " = " + strings.Join(values, " | "))
} else if packageConstantTypeValuesString, ok := packageConstantTypeValues.(string); ok {
packageCodeMap[packageConstantTypeName].l("export type " + packageConstantTypeName + " = " + packageConstantTypeValuesString)
}
@ -215,38 +218,6 @@ func renderTypescriptStructsToPackages(
mappedTypeScript[mapping.GoPackage][structName] = structCode
}
}
for packageName, packageConstants := range constants {
if len(packageConstants) > 0 {
ensureCodeInPackage(packageName)
_, done := mappedTypeScript[packageName][goConstPseudoPackage]
if done {
continue
}
constCode := newCode(" ")
if moduleKind != config.ModuleKindCommonJS {
constCode.ind(1)
}
constCode.l("// constants from " + packageName).l("export const GoConst = {").ind(1)
// constCode.l()
mappedTypeScript[packageName][goConstPseudoPackage] = constCode
constPrefixParts := split(packageName, []string{"/", ".", "-"})
constPrefix := ""
for _, constPrefixPart := range constPrefixParts {
constPrefix += ucFirst(constPrefixPart)
}
constNames := []string{}
for constName := range packageConstants {
constNames = append(constNames, constName)
}
sort.Strings(constNames)
for _, constName := range constNames {
basicLit := packageConstants[constName]
constCode.l(fmt.Sprint(constName, " : ", basicLit.Value, ","))
}
constCode.ind(-1).l("}")
}
}
return nil
}