소스 검색

今日修改0516

Administrator 2 년 전
부모
커밋
9261d1cf3a
4개의 변경된 파일264개의 추가작업 그리고 76개의 파일을 삭제
  1. 6 7
      main.go
  2. 160 66
      service/RunTest.go
  3. 95 0
      service/TemplateStruct.go
  4. 3 3
      service/struct.go.template

+ 6 - 7
main.go

@@ -2,21 +2,20 @@ package main
 
 import (
 	"bufio"
-	"go-create/configs"
 	"go-create/service"
 	"go-create/tmpl"
-	"go-create/util"
 	"html/template"
 	"os"
 )
 
-func init() {
-	configs.ConfigInit()
-}
+//	func init() {
+//		configs.ConfigInit()
+//	}
 func main() {
 	//初始化
-	util.InitMTableArray(configs.Engine)
-	service.InitTable()
+	//util.InitMTableArray(configs.Engine)
+	//service.InitTable()
+	service.RunTest()
 	//执行生成,go
 	//util.MysqlToStruct()
 	//util.MysqlToDao()

+ 160 - 66
service/RunTest.go

@@ -19,90 +19,183 @@ import (
 // - {{.item}}
 // 解析模板
 func RunTest() {
-
 	tmpl := ReadFileToString()
-	//compileRegex := regexp.MustCompile("{{2} *(for *\\w+ +in +\\.\\w+)|(if +\\.\\w+)|(else)|(else +if +\\.\\w+) *}{2}") // 正则表达式的分组,以括号()表示,每一对括号就是我们匹配到的一个文本,可以把他们提取出来。
-	//matchArr := compileRegex.FindStringSubmatch(tmpl)                                                                         // FindStringSubmatch 方法是提取出匹配的字符串,然后通过[]string返回。我们可以看到,第1个匹配到的是这个字符串本身,从第2个开始,才是我们想要的字符串。
-	//if len(matchArr) > 0 {
-	//	fmt.Println("提取字符串内容:", matchArr[len(matchArr)-1]) // 输出:蜜桃乌龙茶
-	//}
-	var err error
-	AllTagReg, err = regexp.Compile("^{{2} *(for *\\w+ +in +\\.\\w+)|(if +\\.\\w+)|(else)|(else +if +\\.\\w+) *}{2}$")
-	if err != nil {
-		panic(err)
-	}
-	ForReg, err = regexp.Compile("^{{2} *for *\\w+ +in +\\.\\w+ *}{2}$")
-	if err != nil {
-		panic(err)
-	}
-	IfReg, err = regexp.Compile("^{{2} *if +\\.\\w+ *}{2}$")
-	if err != nil {
-		panic(err)
-	}
-	IfElseReg, err = regexp.Compile("^{{2} *else +if +\\.\\w+ *}{2}$")
-	if err != nil {
-		panic(err)
+	tags, _ := AnalysisTemplateTags(tmpl)
+	//end, i := GetEnd(tags, 15, len(tags))
+	//fmt.Println(end, i)
+	read := Read(tags)
+	fmt.Println(read)
+	//FindDoubleTagEnd(tags)
+	//template, _ := AnalysisTemplate(tmpl, tags)
+	//fmt.Println(template)
+}
+
+func Read(tags []Tag) []TagDouble {
+	tds := make([]TagDouble, 0)
+	var end TagDouble
+	var i = 0
+	//首层
+	for i < len(tags) {
+		end, i = GetEnd(tags, i, len(tags))
+
+		if end.IsDoubleTag && len(end.OtherTag) > 0 {
+			end.SubTags = append(end.SubTags, Read(end.OtherTag)...)
+			fmt.Print("")
+		}
+		tds = append(tds, end)
 	}
-	ElseReg, err = regexp.Compile("^{{2} *else *}{2}$")
-	if err != nil {
-		panic(err)
+	return tds
+}
+
+// GetEnd 结果,返回数组坐标
+func GetEnd(tags []Tag, startIndex, endIndex int) (TagDouble, int) {
+	fmt.Println(tags, startIndex, endIndex)
+	sign := 0
+	otherTags := make([]Tag, 0)
+	head := tags[startIndex]
+	if head.TagType == "ITEM" {
+		return TagDouble{
+			HeadTag:     tags[startIndex],
+			IsDoubleTag: false,
+			EndTag:      Tag{},
+			OtherTag:    nil,
+			SubTags:     nil,
+		}, startIndex + 1
 	}
-	End, err = regexp.Compile("^{{2} *end *}{2}$")
-	if err != nil {
-		panic(err)
+
+	for i := startIndex + 1; i < endIndex; i++ {
+		tag := tags[i]
+		if tag.TagType == "IF" || tag.TagType == "FOR" {
+			otherTags = append(otherTags, tag)
+			sign += 1
+		} else if tag.TagType == "END" {
+
+			if sign == 0 {
+				return TagDouble{
+					HeadTag:     head,
+					IsDoubleTag: true,
+					EndTag:      tag,
+					OtherTag:    otherTags,
+					SubTags:     nil,
+				}, i + 1
+			} else {
+				sign -= 1
+				otherTags = append(otherTags, tag)
+			}
+		} else if tag.TagType == "ITEM" {
+			otherTags = append(otherTags, tag)
+		} else {
+			//panic("其他标签不能作为首标签")
+		}
 	}
-	Item, err = regexp.Compile("^{{2} *\\.\\w+ *}{2}$")
+	panic("在咋回事?3")
+}
 
-	template, index, err := AnalysisTemplate(tmpl, 0)
-	if err != nil {
-		fmt.Println(err)
+func IsDoubleHeadTag(Tag Tag) bool {
+	switch strings.ToUpper(Tag.TagType) {
+	case "FOR":
+		return true
+	case "IF":
+		return true
+	case "ELSE":
+		return true
+	case "ELSEIF":
+		return true
 	}
-	fmt.Println(template, index)
+	return false
 }
 
-var AllTagReg *regexp.Regexp
-var ForReg *regexp.Regexp
-var IfReg *regexp.Regexp
-var IfElseReg *regexp.Regexp
-var ElseReg *regexp.Regexp
-var End *regexp.Regexp
-var Item *regexp.Regexp
-
-type Template struct {
-	Tag         string //标签
-	TagType     string //标签类型 for if
-	Content     string //
-	SubTemplate []Template
+type TagDouble struct {
+	HeadTag     Tag         //头部标签
+	IsDoubleTag bool        //是否为双标签
+	EndTag      Tag         //结束标签
+	OtherTag    []Tag       //同级并列标签,用于if标签
+	SubTags     []TagDouble //子标签
 }
 
-func (t Template) String() string {
-	return fmt.Sprintf("Tag:%s,TagType:%s,Content:%s,SubTemplate:%s", t.Tag, t.TagType, t.Content, t.SubTemplate)
+func (t TagDouble) String() string {
+	return fmt.Sprintf("TagDouble{HeadTag:%s,IsDoubleTag:%t,EndTag:%s,OtherTag:%s,SubTags:%s}", t.HeadTag.String(), t.IsDoubleTag, t.EndTag.String(), t.OtherTag, t.SubTags)
 }
 
-// AnalysisTemplate
-func AnalysisTemplate(tmpl string, startIndex int) (Template, int, error) {
-	template := Template{}
-	index := IndexOf(tmpl, "{{", startIndex)
+// AnalysisTemplateTags 解析模板中所有的标签
+func AnalysisTemplateTags(tmpl string) ([]Tag, int) {
+	var tags []Tag
+	return FindTag(tmpl, tags, 0)
+}
+
+func FindTag(tmpl string, tags []Tag, startIndex int) ([]Tag, int) {
+	if startIndex == -1 {
+		return tags, -1
+	}
+	index := FindTagSign(tmpl, startIndex, true)
 	if index == -1 {
 		//完美
-		return template, -1, nil
+		return tags, -1
 	}
-	endIndex := IndexOf(tmpl, "}}", index+1)
+	endIndex := FindTagSign(tmpl, index, false)
 	if endIndex == -1 {
-		return template, -1, nil
+		return tags, -1
 	}
 	str := tmpl[index : endIndex+2]
 	//正则判断
-	if AllTagReg.MatchString(str) {
-		compileRegex := regexp.MustCompile("\\{\\{ *(for|if|elseif|else|end) +.* *}}")
-		matchArr := compileRegex.FindStringSubmatch(str)
-		template.TagType = matchArr[1]
-		//匹配到
-		template.Tag = str
-		template.Content = tmpl[index+2 : endIndex]
-		return template, endIndex + 2, nil
+	if !AllTagReg.MatchString(str) {
+		return tags, -1
+	}
+	compileRegex := regexp.MustCompile("\\{\\{ *(for|if|elseif|else|end|FOR|IF|ELSEIF|ELSE|END) *.* *}}")
+	matchArr := compileRegex.FindStringSubmatch(str)
+	tag := "ITEM"
+	if len(matchArr) >= 2 {
+		tag = matchArr[1]
+	}
+	tags = append(tags, Tag{
+		Template:   nil,
+		Tag:        str,
+		StartIndex: startIndex,
+		EndIndex:   endIndex + 2,
+		TagType:    strings.ToUpper(tag),
+	})
+	return FindTag(tmpl, tags, endIndex+2)
+}
+
+//func AnalysisTemplate(tmpl string, startIndex int) (Template, int) {
+//	template := Template{}
+//	index := FindTagSign(tmpl, startIndex, true)
+//	if index == -1 {
+//		//完美
+//		return template, -1
+//	}
+//	endIndex := FindTagSign(tmpl, startIndex, false)
+//	if endIndex == -1 {
+//		return template, -1
+//	}
+//	str := tmpl[index : endIndex+2]
+//	//正则判断
+//	if AllTagReg.MatchString(str) {
+//		//提取标签
+//		compileRegex := regexp.MustCompile("\\{\\{ *(for|if|elseif|else|end) +.* *}}")
+//		matchArr := compileRegex.FindStringSubmatch(str)
+//
+//		template.TagType = matchArr[1]
+//		//匹配到
+//		template.Tag = Tag{
+//			Template:   &template,
+//			tag:        "",
+//			StartIndex: 0,
+//			EndIndex:   0,
+//			TagType:    "",
+//		}
+//		template.Content = tmpl[index+2 : endIndex]
+//		return template, endIndex + 2
+//	}
+//	return Template{}, index
+//}
+
+// FindTagSign 查找模板头部 todo 如果后续需要做转义处理,可以在这里完成
+func FindTagSign(tmpl string, startIndex int, isHead bool) int {
+	if isHead {
+		return IndexOf(tmpl, "{{", startIndex)
 	}
-	return Template{}, index, nil
+	return IndexOf(tmpl, "}}", startIndex)
 }
 
 func IndexOf(s, subString string, startIndex int) int {
@@ -122,15 +215,16 @@ func ReadFileToString() string {
 	defer file.Close()
 	reader := bufio.NewReader(file)
 	var bytes = make([]byte, 1024)
+	var strBytes = make([]byte, 0)
 	for {
 		n, err := reader.Read(bytes)
 		if err != nil {
 			panic(err)
 		}
-		bytes = append(bytes, bytes[:n]...)
+		strBytes = append(strBytes, bytes[:n]...)
 		if n < 1024 {
 			break
 		}
 	}
-	return string(bytes)
+	return string(strBytes)
 }

+ 95 - 0
service/TemplateStruct.go

@@ -0,0 +1,95 @@
+package service
+
+import (
+	"fmt"
+	"regexp"
+)
+
+func init() {
+	var err error
+	AllTagReg, err = regexp.Compile("^{{2} *((for|FOR) *\\w+ +(in|IN) +(\\.\\w+)+})|((if|IF) +(\\.\\w+)+)|(else)|(ELSE)|((elseif)|(ELSEIF) +(\\.\\w+)+)|(end)|(END)|((\\.\\w+)+) *}{2}$")
+	if err != nil {
+		panic(err)
+	}
+	ForReg, err = regexp.Compile("^{{2} *(for|FOR) *\\w+ +(in|IN) +(\\.\\w+)+ *}{2}$")
+	if err != nil {
+		panic(err)
+	}
+	IfReg, err = regexp.Compile("^{{2} *(if|IF) +(\\.\\w+)+ *}{2}$")
+	if err != nil {
+		panic(err)
+	}
+	IfElseReg, err = regexp.Compile("^{{2} *(elseif)|(ELSEIF) +(\\.\\w+)+ *}{2}$")
+	if err != nil {
+		panic(err)
+	}
+	ElseReg, err = regexp.Compile("^{{2} *(else|ELSE) *}{2}$")
+	if err != nil {
+		panic(err)
+	}
+	EndReg, err = regexp.Compile("^{{2} *(end|END) *}{2}$")
+	if err != nil {
+		panic(err)
+	}
+	ItemReg, err = regexp.Compile("^{{2} *(\\.\\w+)+ *}{2}$")
+	if err != nil {
+		panic(err)
+	}
+}
+
+var AllTagReg *regexp.Regexp
+var ForReg *regexp.Regexp
+var IfReg *regexp.Regexp
+var IfElseReg *regexp.Regexp
+var ElseReg *regexp.Regexp
+var EndReg *regexp.Regexp
+var ItemReg *regexp.Regexp
+
+// CheckTag 校验字符串是否符合标签
+func CheckTag(str, tag string) bool {
+	var res = false
+	switch tag {
+	case "FOR":
+		res = ForReg.MatchString(str)
+		break
+	case "IF":
+		res = IfReg.MatchString(str)
+		break
+	case "IFELSE":
+		res = IfElseReg.MatchString(str)
+		break
+	case "ELSE":
+		res = ElseReg.MatchString(str)
+		break
+	case "END":
+		res = EndReg.MatchString(str)
+		break
+	case "ITEM":
+		res = ItemReg.MatchString(str)
+		break
+	}
+	return res
+}
+
+type Template struct {
+	Tag         []Tag      //标签
+	TagType     string     //标签类型 for if
+	Content     string     //模板内容
+	SubTemplate []Template //模板
+}
+
+func (t Template) String() string {
+	return fmt.Sprintf("Tag:%s,TagType:%s,Content:%s,SubTemplate:%s", t.Tag, t.TagType, t.Content, t.SubTemplate)
+}
+
+type Tag struct {
+	Template   *Template
+	Tag        string
+	StartIndex int
+	EndIndex   int
+	TagType    string
+}
+
+func (t Tag) String() string {
+	return fmt.Sprintf("Tag:%s,StartIndex:%d,EndIndex:%d,TagType:%s\n", t.Tag, t.StartIndex, t.EndIndex, t.TagType)
+}

+ 3 - 3
service/struct.go.template

@@ -4,14 +4,14 @@ import (
     "fmt"
     "time"
 )
-{{for itemfor in .forlist}}
+{{for item in .forlist}}
 // {{.GreatHump}} {{.Comment}}
 type {{.GreatHump}} struct {
-{{range .MColumns}}    {{.GreatHump}} {{.TypeName}} `{{if .IsKey}} {{else}}xorm:"{{.Name}}"{{end}} json:"{{.GreatHump}}"`
+{{for col in .MColumns}}    {{.GreatHump}} {{.TypeName}} `{{if .IsKey}} {{else}}xorm:"{{.Name}}"{{end}} json:"{{.GreatHump}}"`
 {{end}}}
 
 func (receiver {{.GreatHump}}) String() string {
-	return fmt.Sprint("{{.GreatHump}}{", {{range .MColumns}}"{{.GreatHump}}:", receiver.{{.GreatHump}},{{end}} "}")
+	return fmt.Sprint("{{.GreatHump}}{", {{for col in .MColumns}}"{{.GreatHump}}:", receiver.{{.GreatHump}},{{end}} "}")
 }
 
 {{end}}