85 строки
1,5 КиБ
Go
85 строки
1,5 КиБ
Go
package service
|
|
|
|
import (
|
|
"go/ast"
|
|
)
|
|
|
|
func handleValueSpec(s *ast.ValueSpec) (code string) {
|
|
if s.Type == nil {
|
|
code += addTypeByValue(s)
|
|
}
|
|
|
|
code += handleValueSpecType(s.Type)
|
|
code += " "
|
|
|
|
_, ok := s.Type.(*ast.StarExpr)
|
|
if ok {
|
|
isPointerType = true
|
|
}
|
|
code += handleValueSpecNames(s.Names)
|
|
isPointerType = false
|
|
|
|
if s.Values != nil {
|
|
code += " = "
|
|
code += handleValueSpecValues(s.Values)
|
|
}
|
|
return code
|
|
}
|
|
|
|
func addTypeByValue(s *ast.ValueSpec) (code string) {
|
|
if len(s.Values) == 0 {
|
|
return
|
|
}
|
|
|
|
value := s.Values[0]
|
|
|
|
switch v := value.(type) {
|
|
case *ast.BasicLit:
|
|
code += handleBasicLitType(v)
|
|
}
|
|
return
|
|
}
|
|
|
|
func handleValueSpecNames(names []*ast.Ident) (code string) {
|
|
nado_zapyatuyu := false
|
|
for _, name := range names {
|
|
if nado_zapyatuyu {
|
|
code += ","
|
|
}
|
|
if isPointerType {
|
|
code += "*"
|
|
}
|
|
code += handleIdentExpr(name)
|
|
nado_zapyatuyu = true
|
|
}
|
|
return
|
|
}
|
|
|
|
func handleValueSpecType(expr ast.Expr) (code string) {
|
|
switch t := expr.(type) {
|
|
case *ast.SelectorExpr:
|
|
code += handleSelectorExpr(t)
|
|
case *ast.Ident:
|
|
code += handleIdentExpr(t)
|
|
case *ast.StarExpr:
|
|
code += handleStarExpr(t)
|
|
}
|
|
return
|
|
}
|
|
|
|
func handleValueSpecValues(values []ast.Expr) string {
|
|
code := ""
|
|
for _, value := range values {
|
|
switch v := value.(type) {
|
|
case *ast.BasicLit:
|
|
code += handleBasicLit(v)
|
|
case *ast.BinaryExpr:
|
|
code += handleBinaryExpr(v)
|
|
case *ast.SelectorExpr:
|
|
code += handleSelectorExpr(value)
|
|
case *ast.CallExpr:
|
|
code += handleCallExpr(v)
|
|
}
|
|
}
|
|
return code
|
|
}
|