|
|
package SyncDao
|
|
|
|
|
|
import (
|
|
|
"dsAutoCode/Utils/CommonUtil"
|
|
|
"dsAutoCode/Utils/DbUtil"
|
|
|
"dsAutoCode/Utils/FileUtil"
|
|
|
"dsAutoCode/models"
|
|
|
"encoding/json"
|
|
|
"fmt"
|
|
|
"github.com/holdno/snowFlakeByGo"
|
|
|
"go/ast"
|
|
|
"go/parser"
|
|
|
"go/token"
|
|
|
"io/ioutil"
|
|
|
"log"
|
|
|
"os"
|
|
|
"path/filepath"
|
|
|
"reflect"
|
|
|
"strings"
|
|
|
"unicode"
|
|
|
)
|
|
|
|
|
|
var db = DbUtil.Engine
|
|
|
|
|
|
/**
|
|
|
功能:清空表格
|
|
|
作者:黄海
|
|
|
时间:2020-04-21
|
|
|
*/
|
|
|
func ProtoTableClear() {
|
|
|
//禁用外键约束
|
|
|
sql := "SET foreign_key_checks=0"
|
|
|
db.Exec(sql)
|
|
|
//1、清空
|
|
|
sql = "truncate table t_proto_files"
|
|
|
db.Exec(sql)
|
|
|
sql = "truncate table t_proto_interface"
|
|
|
db.Exec(sql)
|
|
|
sql = "truncate table t_proto_struct"
|
|
|
db.Exec(sql)
|
|
|
sql = "truncate table t_proto_struct_parameter"
|
|
|
db.Exec(sql)
|
|
|
sql = "truncate table t_swagger_controller"
|
|
|
db.Exec(sql)
|
|
|
sql = "truncate table t_swagger_controller_parameter"
|
|
|
db.Exec(sql)
|
|
|
//禁用外键约束
|
|
|
sql = "SET foreign_key_checks=1"
|
|
|
db.Exec(sql)
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
功能:将proto转化为数据库中
|
|
|
作者:黄海
|
|
|
时间:2020-04-22
|
|
|
*/
|
|
|
func ProtoTableToDb() {
|
|
|
var IdWorker *snowFlakeByGo.Worker
|
|
|
IdWorker, _ = snowFlakeByGo.NewWorker(0)
|
|
|
//声明事务
|
|
|
session := db.NewSession()
|
|
|
defer session.Close()
|
|
|
//启动事务
|
|
|
err := session.Begin()
|
|
|
if err != nil {
|
|
|
log.Fatalln(err)
|
|
|
}
|
|
|
//遍历所有proto文件
|
|
|
pathname := CommonUtil.GetCurrentParentPath()+"/dsBaseWeb/Business"
|
|
|
files, _ := FileUtil.WalkDir(pathname, ".pb.go")
|
|
|
for m := 0; m < len(files); m++ {
|
|
|
//临时变量
|
|
|
_map := make(map[string]int64, 1)
|
|
|
|
|
|
//1、读取go源文件
|
|
|
b, _ := ioutil.ReadFile(files[m])
|
|
|
var protoFile models.TProtoFiles
|
|
|
protoFile.FileId = CommonUtil.ConvertInt64ToString(IdWorker.GetId())
|
|
|
protoFile.FilePath, protoFile.FileName = filepath.Split(files[m])
|
|
|
//通过事务插入数据
|
|
|
_, err = session.Insert(protoFile)
|
|
|
if err != nil {
|
|
|
session.Rollback()
|
|
|
log.Fatalln(err)
|
|
|
}
|
|
|
//2、解析.go文件, Go!
|
|
|
src := string(b)
|
|
|
fset := token.NewFileSet()
|
|
|
f, err := parser.ParseFile(fset, "", src, 0)
|
|
|
|
|
|
if err != nil {
|
|
|
panic(err)
|
|
|
}
|
|
|
//解析.go文件
|
|
|
for i := 0; i < len(f.Decls); i++ {
|
|
|
if reflect.TypeOf(f.Decls[i]).String() == "*ast.GenDecl" {
|
|
|
typeDecl := f.Decls[i].(*ast.GenDecl)
|
|
|
for j := 0; j < len(typeDecl.Specs); j++ {
|
|
|
if reflect.TypeOf(typeDecl.Specs[j]).String() == "*ast.TypeSpec" {
|
|
|
structName := typeDecl.Specs[j].(*ast.TypeSpec).Name
|
|
|
//未实现的为代码自动生成,不参与运算
|
|
|
if strings.Index(structName.Name, "Unimplemented") == 0 {
|
|
|
continue
|
|
|
}
|
|
|
// 首字母小写的不参加运算.小写的不是我们生成的结构体,我们就是这么要求的~
|
|
|
if !unicode.IsUpper([]rune(structName.Name)[0]) {
|
|
|
continue
|
|
|
}
|
|
|
//如果是struct,表示是输入输出参数列表
|
|
|
if reflect.TypeOf(typeDecl.Specs[j].(*ast.TypeSpec).Type).String() == "*ast.StructType" {
|
|
|
var protoStruct models.TProtoStruct
|
|
|
protoStruct.StructId = CommonUtil.ConvertInt64ToString(IdWorker.GetId())
|
|
|
protoStruct.FileId = protoFile.FileId
|
|
|
protoStruct.StructName = structName.Name
|
|
|
//通过事务插入数据
|
|
|
_, err = session.Insert(protoStruct)
|
|
|
if err != nil {
|
|
|
session.Rollback()
|
|
|
log.Fatalln(err)
|
|
|
}
|
|
|
_map[structName.Name] = CommonUtil.ConvertStringToInt64(protoStruct.StructId)
|
|
|
//属性
|
|
|
if reflect.TypeOf(typeDecl.Specs[j].(*ast.TypeSpec).Type).String() == "*ast.StructType" {
|
|
|
structDecl := typeDecl.Specs[j].(*ast.TypeSpec).Type.(*ast.StructType)
|
|
|
fields := structDecl.Fields.List
|
|
|
for _, v := range fields {
|
|
|
//支持字符串数组
|
|
|
if reflect.TypeOf(v.Type).String() == "*ast.ArrayType" {
|
|
|
fieldName := v.Names[0].Name
|
|
|
typeExpr := "string"
|
|
|
var protoStructParameter models.TProtoStructParameter
|
|
|
protoStructParameter.StructId = protoStruct.StructId
|
|
|
protoStructParameter.StructParameterId = CommonUtil.ConvertInt64ToString(IdWorker.GetId())
|
|
|
protoStructParameter.StructParameterName = fieldName
|
|
|
protoStructParameter.StructParameterType = typeExpr
|
|
|
protoStructParameter.IsRepeate = 1
|
|
|
//通过事务插入数据
|
|
|
_, err = session.Insert(protoStructParameter)
|
|
|
if err != nil {
|
|
|
session.Rollback()
|
|
|
log.Fatalln(err)
|
|
|
}
|
|
|
}
|
|
|
//普通简单类型
|
|
|
if reflect.TypeOf(v.Type).String() == "*ast.Ident" {
|
|
|
fieldName := v.Names[0].Name
|
|
|
typeExpr := v.Type
|
|
|
var protoStructParameter models.TProtoStructParameter
|
|
|
protoStructParameter.StructId = protoStruct.StructId
|
|
|
protoStructParameter.StructParameterId = CommonUtil.ConvertInt64ToString(IdWorker.GetId())
|
|
|
protoStructParameter.StructParameterName = fieldName
|
|
|
protoStructParameter.StructParameterType = fmt.Sprintf("%s", typeExpr)
|
|
|
protoStructParameter.IsRepeate = 0
|
|
|
//通过事务插入数据
|
|
|
_, err = session.Insert(protoStructParameter)
|
|
|
if err != nil {
|
|
|
session.Rollback()
|
|
|
log.Fatalln(err)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
//如果是inerface,表示是接口列表
|
|
|
if reflect.TypeOf(typeDecl.Specs[j].(*ast.TypeSpec).Type).String() == "*ast.InterfaceType" {
|
|
|
//这个是自动生成的,需要处理
|
|
|
if strings.HasSuffix(structName.Name, "Server") {
|
|
|
//解析下面定义的接口有哪些?
|
|
|
service := typeDecl.Specs[j].(*ast.TypeSpec).Type.(*ast.InterfaceType)
|
|
|
for k := 0; k < len(service.Methods.List); k++ {
|
|
|
//这个方法的入参是什么?回参是统一的
|
|
|
serviceName := service.Methods.List[k].Names[0].Name
|
|
|
argName := service.Methods.List[k].Type.(*ast.FuncType).Params.List[1].Type.(*ast.StarExpr).X.(*ast.Ident).Name
|
|
|
//美丽的对齐字符串
|
|
|
var protoInterface models.TProtoInterface
|
|
|
//根据参数名需要知道argName--->struct_id是多少?
|
|
|
protoInterface.StructId = CommonUtil.ConvertInt64ToString(_map[argName])
|
|
|
protoInterface.FileId = protoFile.FileId
|
|
|
protoInterface.InterfaceId = CommonUtil.ConvertInt64ToString(IdWorker.GetId())
|
|
|
protoInterface.InterfaceName = serviceName
|
|
|
//通过事务插入数据
|
|
|
_, err = session.Insert(protoInterface)
|
|
|
if err != nil {
|
|
|
session.Rollback()
|
|
|
log.Fatalln(err)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
//事务
|
|
|
err = session.Commit()
|
|
|
if err != nil {
|
|
|
log.Fatalln(err)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
功能:清空表格
|
|
|
作者:黄海
|
|
|
时间:2020-04-21
|
|
|
*/
|
|
|
func SwaggerTableClear() {
|
|
|
//禁用外键约束
|
|
|
sql := "SET foreign_key_checks=0"
|
|
|
db.Exec(sql)
|
|
|
//1、清空
|
|
|
sql = "truncate table t_swagger_controller"
|
|
|
db.Exec(sql)
|
|
|
sql = "truncate table t_swagger_controller_parameter"
|
|
|
db.Exec(sql)
|
|
|
//禁用外键约束
|
|
|
sql = "SET foreign_key_checks=1"
|
|
|
db.Exec(sql)
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
功能:读取swagger的生成json
|
|
|
作者:黄海
|
|
|
时间:2020-03-16
|
|
|
*/
|
|
|
func readSwaggerJson() interface{} {
|
|
|
//读取doc目录下的swagger.json
|
|
|
parentDir:=CommonUtil.GetParentPath(CommonUtil.GetCurrentDirectory())
|
|
|
swaggerFile := parentDir+"/dsBaseWeb/docs/swagger.json"
|
|
|
f, err := os.Open(swaggerFile)
|
|
|
if err != nil {
|
|
|
log.Fatal("读取swagger.json失败!")
|
|
|
return nil
|
|
|
}
|
|
|
content, _ := ioutil.ReadAll(f)
|
|
|
var jsonBody interface{}
|
|
|
err = json.Unmarshal(content, &jsonBody)
|
|
|
if err != nil {
|
|
|
fmt.Println("ERROR: ", err.Error())
|
|
|
return nil
|
|
|
}
|
|
|
return jsonBody
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
功能:将swagger配置文件中的json数据整理入库
|
|
|
作者:黄海
|
|
|
时间:2020-04-22
|
|
|
*/
|
|
|
func SwaggerTableToDb() {
|
|
|
//读取json文件
|
|
|
jsonBody := readSwaggerJson()
|
|
|
var IdWorker *snowFlakeByGo.Worker
|
|
|
IdWorker, _ = snowFlakeByGo.NewWorker(0)
|
|
|
|
|
|
m := jsonBody.(map[string]interface{})
|
|
|
n := m["paths"].(map[string]interface{})
|
|
|
for k, v := range n {
|
|
|
t := v.(map[string]interface{})
|
|
|
httpType := "post"
|
|
|
//非空检查器
|
|
|
if t["get"] != nil {
|
|
|
httpType = "get"
|
|
|
}
|
|
|
r1 := t[httpType].(map[string]interface{})
|
|
|
//描述
|
|
|
description := r1["description"].(string)
|
|
|
|
|
|
var sc models.TSwaggerController
|
|
|
sc.ControllerId = CommonUtil.ConvertInt64ToString(IdWorker.GetId())
|
|
|
t1 := strings.Split(k, "/")
|
|
|
sc.ControllerName = t1[len(t1)-1]
|
|
|
|
|
|
if r1["x-interfacename"] != nil {
|
|
|
var a = r1["x-interfacename"].([]interface{})
|
|
|
sc.InterfaceName = a[0].(string)
|
|
|
} else {
|
|
|
sc.InterfaceName = t1[len(t1)-1]
|
|
|
}
|
|
|
sc.Description = description
|
|
|
sc.HttpType = httpType
|
|
|
_, err := db.Insert(sc)
|
|
|
if err != nil {
|
|
|
log.Fatalln(err)
|
|
|
}
|
|
|
//参数
|
|
|
if r1["parameters"]!=nil{
|
|
|
parameters := r1["parameters"].([]interface{})
|
|
|
//遍历这些参数
|
|
|
for i := 0; i < len(parameters); i++ {
|
|
|
parameter := parameters[i].(map[string]interface{})
|
|
|
var swaggerParameter models.TSwaggerControllerParameter
|
|
|
swaggerParameter.ControllerId = sc.ControllerId
|
|
|
swaggerParameter.ControllerParameterGetType = parameter["in"].(string)
|
|
|
swaggerParameter.ControllerParameterId = CommonUtil.ConvertInt64ToString(IdWorker.GetId())
|
|
|
swaggerParameter.ControllerParameterName = parameter["name"].(string)
|
|
|
var isRequird bool
|
|
|
if _, ok := parameter["required"]; ok {
|
|
|
isRequird = parameter["required"].(bool)
|
|
|
} else {
|
|
|
isRequird = false
|
|
|
}
|
|
|
if isRequird {
|
|
|
swaggerParameter.IsRequired = 1
|
|
|
} else {
|
|
|
swaggerParameter.IsRequired = 0
|
|
|
}
|
|
|
//描述
|
|
|
swaggerParameter.ControllerParameterDescription = parameter["description"].(string)
|
|
|
swaggerParameter.ControllerParameterType = parameter["type"].(string)
|
|
|
_, err := db.Insert(swaggerParameter)
|
|
|
if err != nil {
|
|
|
log.Fatalln(err)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|