go-translator/pkg/service/spec.go

51 строка
943 Б
Go

package service
import (
"go/ast"
"go/token"
)
var InConstBlock = false
func handleGenDecl(decl ast.Decl) string {
gd := decl.(*ast.GenDecl)
code := ""
switch gd.Tok {
case token.CONST:
InConstBlock = true
code += handleSpecs(gd.Specs)
InConstBlock = false
default:
code += handleSpecs(gd.Specs)
}
return code
}
func handleSpecs(specs []ast.Spec) (code string) {
if InConstBlock {
return handleConstSpecs(specs)
}
return handleSpecsVariables(specs)
}
func handleSpecsVariables(specs []ast.Spec) (code string) {
for _, spec := range specs {
switch s := spec.(type) {
case *ast.ImportSpec:
code += handleImportSpec(s)
case *ast.ValueSpec:
code += handleGlobalOrLocalVariable(s)
case *ast.TypeSpec:
code += handleTypeSpec(s)
}
}
return
}
func handleGlobalOrLocalVariable(s *ast.ValueSpec) (code string) {
if IsInFunction() {
return handleLocalVariable(s)
}
return handleGlobalVariable(s)
}