Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions tars/tools/tars2go/ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,6 @@ func (m *Module) Rename(moduleUpper bool) {
}
}

func (tf *TarsFile) Rename(moduleUpper bool) {
tf.Module.Rename(moduleUpper)
}

// FindTNameType Looking for the true type of user-defined identifier
func (tf *TarsFile) FindTNameType(tName string) (token.Type, string, string) {
for _, v := range tf.Module.Struct {
Expand Down
40 changes: 21 additions & 19 deletions tars/tools/tars2go/gencode/gen_go.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type GenGo struct {
filepath string
prefix string
tarsFile *ast.TarsFile
module *ast.Module

// proto file name(not include .tars)
ProtoName string
Expand Down Expand Up @@ -96,8 +97,9 @@ func (g *GenGo) W(v ...string) {
}

func (g *GenGo) genAll() {
g.tarsFile.Rename(g.opt.ModuleUpper)
key := g.tarsFile.Source + ":" + g.tarsFile.Module.Name
g.module = &g.tarsFile.Module
g.module.Rename(g.opt.ModuleUpper)
key := g.tarsFile.Source + ":" + g.module.Name
if _, ok := fileMap.Load(key); ok {
// already compiled
return
Expand All @@ -110,20 +112,20 @@ func (g *GenGo) genAll() {
g.genHead()
g.genPackage()

for _, v := range g.tarsFile.Module.Enum {
for _, v := range g.module.Enum {
g.genEnum(&v)
}

g.genConst(g.tarsFile.Module.Const)
g.genConst(g.module.Const)

for _, v := range g.tarsFile.Module.Struct {
for _, v := range g.module.Struct {
g.genStruct(&v)
}
if len(g.tarsFile.Module.Enum) > 0 || len(g.tarsFile.Module.Const) > 0 || len(g.tarsFile.Module.Struct) > 0 {
if len(g.module.Enum) > 0 || len(g.module.Const) > 0 || len(g.module.Struct) > 0 {
g.saveToSourceFile(utils.Path2ProtoName(g.filepath) + ".go")
}

for _, v := range g.tarsFile.Module.Interface {
for _, v := range g.module.Interface {
g.genInterface(&v)
}
}
Expand Down Expand Up @@ -156,9 +158,9 @@ func (g *GenGo) saveToSourceFile(filename string) {
} else {
var mkPath string
if g.opt.ModuleCycle {
mkPath = prefix + g.ProtoName + "/" + g.tarsFile.Module.Name
mkPath = prefix + g.ProtoName + "/" + g.module.Name
} else {
mkPath = prefix + g.tarsFile.Module.Name
mkPath = prefix + g.module.Name
}
err = os.MkdirAll(mkPath, 0766)
if err != nil {
Expand All @@ -182,11 +184,11 @@ func (g *GenGo) genVariableName(prefix, name string) string {
func (g *GenGo) genHead() {
g.P("// Code generated by tars2go ", version.VERSION, ", DO NOT EDIT.")
g.P("// This file was generated from ", g.tarsFile.Source)
g.P("// Package ", g.tarsFile.Module.Name, " comment")
g.P("// Package ", g.module.Name, " comment")
}

func (g *GenGo) genPackage() {
g.P("package ", g.tarsFile.Module.Name)
g.P("package ", g.module.Name)
g.P()
g.P("import (")
g.P(strconv.Quote("fmt"))
Expand All @@ -195,7 +197,7 @@ func (g *GenGo) genPackage() {

g.P()
mImports := make(map[string]bool)
for _, st := range g.tarsFile.Module.Struct {
for _, st := range g.module.Struct {
if g.opt.ModuleCycle {
for k, v := range st.DependModuleWithJce {
g.genStructImport(k, v, mImports)
Expand Down Expand Up @@ -262,7 +264,7 @@ func (g *GenGo) genStructImport(module string, protoName string, mImports map[st
}

func (g *GenGo) genIFPackage(itf *ast.Interface) {
g.P("package " + g.tarsFile.Module.Name)
g.P("package " + g.module.Name)
g.P()

g.P("import (")
Expand Down Expand Up @@ -944,18 +946,18 @@ func (g *GenGo) genConst(cst []ast.Const) {

g.P("//const as define in tars file")
g.P("const (")
for _, v := range g.tarsFile.Module.Const {
for _, v := range g.module.Const {
v.Rename()
g.P(v.Name, " ", g.genType(v.Type), " = ", v.Value)
}
g.P(")")
}

func (g *GenGo) genInclude(modules []*ast.TarsFile) {
for _, module := range modules {
genModule := NewGenGo(g.opt, module.Module.Name+module.Source)
genModule.tarsFile = module
genModule.genAll()
func (g *GenGo) genInclude(tarsFiles []*ast.TarsFile) {
for _, tarsFile := range tarsFiles {
gen := NewGenGo(g.opt, tarsFile.Source)
gen.tarsFile = tarsFile
gen.genAll()
}
}

Expand Down
2 changes: 1 addition & 1 deletion tars/tools/tars2go/version/version.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package version

// VERSION of the tars2go tools.
const VERSION = "1.2.3"
const VERSION = "1.2.4"