93 строки
1,5 КиБ
Go
93 строки
1,5 КиБ
Go
package service
|
|
|
|
import "go/ast"
|
|
|
|
var (
|
|
variableDeclarations []*Variable
|
|
)
|
|
|
|
type Variable struct {
|
|
s *ast.ValueSpec
|
|
}
|
|
|
|
func NewVariable(s *ast.ValueSpec) *Variable {
|
|
return &Variable{s: s}
|
|
}
|
|
|
|
func (c *Variable) String() (code string) {
|
|
code += c.genString(c.s)
|
|
return
|
|
}
|
|
|
|
func (c *Variable) genString(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)
|
|
}
|
|
|
|
l, ok := s.Type.(*ast.ArrayType)
|
|
if ok {
|
|
code += "["
|
|
if l.Len != nil {
|
|
code += handleExpr(l.Len)
|
|
}
|
|
code += "]"
|
|
}
|
|
code += ";"
|
|
return
|
|
}
|
|
|
|
func handleLocalVariable(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)
|
|
}
|
|
|
|
l, ok := s.Type.(*ast.ArrayType)
|
|
if ok {
|
|
code += "["
|
|
if l.Len != nil {
|
|
code += handleExpr(l.Len)
|
|
}
|
|
code += "]"
|
|
}
|
|
code += ";"
|
|
return code
|
|
}
|
|
|
|
func handleGlobalVariable(s *ast.ValueSpec) (code string) {
|
|
addVariable(s)
|
|
return
|
|
}
|
|
func addVariable(s *ast.ValueSpec) {
|
|
c := NewVariable(s)
|
|
variableDeclarations = append(variableDeclarations, c)
|
|
}
|