Skip to content

Commit

Permalink
feat: optimize reflection usage
Browse files Browse the repository at this point in the history
  • Loading branch information
franklinkim committed Aug 17, 2022
1 parent c19ce8f commit c6c51db
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 57 deletions.
12 changes: 6 additions & 6 deletions model.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package gotsrpc
type ScalarType string

const (
ScalarTypeString ScalarType = "string"
ScalarTypeInterface ScalarType = "any"
ScalarTypeByte ScalarType = "byte"
ScalarTypeNumber ScalarType = "number"
ScalarTypeBool ScalarType = "bool"
ScalarTypeNone ScalarType = ""
ScalarTypeString ScalarType = "string"
ScalarTypeAny ScalarType = "any"
ScalarTypeByte ScalarType = "byte"
ScalarTypeNumber ScalarType = "number"
ScalarTypeBool ScalarType = "bool"
ScalarTypeNone ScalarType = ""
)

type JSONInfo struct {
Expand Down
100 changes: 49 additions & 51 deletions typereader.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ func extractJSONInfo(tag string) *JSONInfo {

func getScalarFromAstIdent(ident *ast.Ident) ScalarType {
switch ident.Name {
case "interface":
return ScalarTypeInterface
case "any", "interface":
return ScalarTypeAny
case "string":
return ScalarTypeString
case "bool":
Expand Down Expand Up @@ -180,15 +180,15 @@ func readAstType(v *Value, fieldIdent *ast.Ident, fileImports fileImportSpecMap,

func readAstStarExpr(v *Value, starExpr *ast.StarExpr, fileImports fileImportSpecMap) {
v.IsPtr = true
switch reflect.ValueOf(starExpr.X).Type().String() {
case "*ast.Ident":
switch starExprType := starExpr.X.(type) {
case *ast.Ident:
ident := starExpr.X.(*ast.Ident)
readAstType(v, ident, fileImports, "")
case "*ast.StructType":
case *ast.StructType:
// nested anonymous
readAstStructType(v, starExpr.X.(*ast.StructType), fileImports)
case "*ast.SelectorExpr":
readAstSelectorExpr(v, starExpr.X.(*ast.SelectorExpr), fileImports)
readAstStructType(v, starExprType, fileImports)
case *ast.SelectorExpr:
readAstSelectorExpr(v, starExprType, fileImports)
default:
trace("a pointer on what", reflect.ValueOf(starExpr.X).Type().String())
}
Expand All @@ -198,16 +198,16 @@ func readAstMapType(m *Map, mapType *ast.MapType, fileImports fileImportSpecMap)
trace(" map key", mapType.Key, reflect.ValueOf(mapType.Key).Type().String())
trace(" map value", mapType.Value, reflect.ValueOf(mapType.Value).Type().String())
// key
switch reflect.ValueOf(mapType.Key).Type().String() {
case "*ast.Ident":
_, scalarType := getTypesFromAstType(mapType.Key.(*ast.Ident))
switch keyType := mapType.Key.(type) {
case *ast.Ident:
_, scalarType := getTypesFromAstType(keyType)
m.KeyType = string(scalarType)
m.KeyGoType = mapType.Key.(*ast.Ident).Name
m.KeyGoType = keyType.Name
m.Key = &Value{}
readAstType(m.Key, mapType.Key.(*ast.Ident), fileImports, "")
case "*ast.SelectorExpr":
readAstType(m.Key, keyType, fileImports, "")
case *ast.SelectorExpr:
m.Key = &Value{}
readAstSelectorExpr(m.Key, mapType.Key.(*ast.SelectorExpr), fileImports)
readAstSelectorExpr(m.Key, keyType, fileImports)
default:
// todo: implement support for "*ast.Scalar" type (sca)
// this is important for scalar types in map keys
Expand All @@ -233,13 +233,13 @@ func readAstMapType(m *Map, mapType *ast.MapType, fileImports fileImportSpecMap)
}

func readAstSelectorExpr(v *Value, selectorExpr *ast.SelectorExpr, fileImports fileImportSpecMap) {
switch reflect.ValueOf(selectorExpr.X).Type().String() {
case "*ast.Ident":
switch selExpType := selectorExpr.X.(type) {
case *ast.Ident:
// that could be the package name
//selectorIdent := selectorExpr.X.(*ast.Ident)
// fmt.Println(selectorExpr, selectorExpr.X.(*ast.Ident))
//readAstType(v, selectorExpr.X.(*ast.Ident), fileImports)
readAstType(v, selectorExpr.Sel, fileImports, selectorExpr.X.(*ast.Ident).Name)
readAstType(v, selectorExpr.Sel, fileImports, selExpType.Name)
if v.StructType != nil {
v.StructType.Package = fileImports.getPackagePath(v.StructType.Name)
v.StructType.Name = selectorExpr.Sel.Name
Expand All @@ -262,50 +262,48 @@ func readAstInterfaceType(v *Value, interfaceType *ast.InterfaceType, fileImport

func (v *Value) loadExpr(expr ast.Expr, fileImports fileImportSpecMap) {

switch reflect.ValueOf(expr).Type().String() {
case "*ast.ArrayType":
fieldArray := expr.(*ast.ArrayType)
switch exprType := expr.(type) {
case *ast.ArrayType:
v.Array = &Array{Value: &Value{}}

switch reflect.ValueOf(fieldArray.Elt).Type().String() {
case "*ast.ArrayType":
switch exprEltType := exprType.Elt.(type) {
case *ast.ArrayType:
//readAstArrayType(v.Array.Value, fieldArray.Elt.(*ast.ArrayType), fileImports)
v.Array.Value.loadExpr(fieldArray.Elt.(*ast.ArrayType), fileImports)
case "*ast.Ident":
readAstType(v.Array.Value, fieldArray.Elt.(*ast.Ident), fileImports, "")
case "*ast.StarExpr":
readAstStarExpr(v.Array.Value, fieldArray.Elt.(*ast.StarExpr), fileImports)
case "*ast.MapType":
v.Array.Value.loadExpr(exprEltType, fileImports)
case *ast.Ident:
readAstType(v.Array.Value, exprEltType, fileImports, "")
case *ast.StarExpr:
readAstStarExpr(v.Array.Value, exprEltType, fileImports)
case *ast.MapType:
v.Array.Value.Map = &Map{
Value: &Value{},
}
readAstMapType(v.Array.Value.Map, fieldArray.Elt.(*ast.MapType), fileImports)
case "*ast.SelectorExpr":
readAstSelectorExpr(v.Array.Value, fieldArray.Elt.(*ast.SelectorExpr), fileImports)
case "*ast.StructType":
readAstStructType(v.Array.Value, fieldArray.Elt.(*ast.StructType), fileImports)
case "*ast.InterfaceType":
readAstInterfaceType(v.Array.Value, fieldArray.Elt.(*ast.InterfaceType), fileImports)
readAstMapType(v.Array.Value.Map, exprEltType, fileImports)
case *ast.SelectorExpr:
readAstSelectorExpr(v.Array.Value, exprEltType, fileImports)
case *ast.StructType:
readAstStructType(v.Array.Value, exprEltType, fileImports)
case *ast.InterfaceType:
readAstInterfaceType(v.Array.Value, exprEltType, fileImports)
default:
trace("---------------------> array of", reflect.ValueOf(fieldArray.Elt).Type().String())
trace("---------------------> array of", reflect.ValueOf(exprType.Elt).Type().String())
}
case "*ast.Ident":
fieldIdent := expr.(*ast.Ident)
readAstType(v, fieldIdent, fileImports, "")
case "*ast.StarExpr":
case *ast.Ident:
readAstType(v, exprType, fileImports, "")
case *ast.StarExpr:
// a pointer on sth
readAstStarExpr(v, expr.(*ast.StarExpr), fileImports)
case "*ast.MapType":
readAstStarExpr(v, exprType, fileImports)
case *ast.MapType:
v.Map = &Map{
Value: &Value{},
}
readAstMapType(v.Map, expr.(*ast.MapType), fileImports)
case "*ast.SelectorExpr":
readAstSelectorExpr(v, expr.(*ast.SelectorExpr), fileImports)
case "*ast.StructType":
readAstStructType(v, expr.(*ast.StructType), fileImports)
case "*ast.InterfaceType":
readAstInterfaceType(v, expr.(*ast.InterfaceType), fileImports)
readAstMapType(v.Map, exprType, fileImports)
case *ast.SelectorExpr:
readAstSelectorExpr(v, exprType, fileImports)
case *ast.StructType:
readAstStructType(v, exprType, fileImports)
case *ast.InterfaceType:
readAstInterfaceType(v, exprType, fileImports)
default:
trace("what kind of field ident would that be ?!", reflect.ValueOf(expr).Type().String())
}
Expand Down Expand Up @@ -426,7 +424,7 @@ func extractTypes(file *ast.File, packageName string, structs map[string]*Struct
scalars[structName] = &Scalar{
Name: structName,
Package: packageName,
Type: ScalarTypeInterface,
Type: ScalarTypeAny,
}
case "*ast.Ident":
trace("Scalar", obj.Name)
Expand Down

0 comments on commit c6c51db

Please sign in to comment.