Example #1
0
func (g *Generator) generateOperations(buf *bytes.Buffer, specDoc *spec.Document) {
	g.Buffer = buf
	paths := specDoc.AllPaths()

	g.p("package operations")
	g.p()

	for _, path := range paths {
		operations := path.PathItemProps
		if post := operations.Post; post != nil {
			g.generateOperation(post)
		}
		if get := operations.Get; get != nil {
			g.generateOperation(get)
		}
		if put := operations.Put; put != nil {
			g.generateOperation(put)
		}
		if del := operations.Delete; del != nil {
			g.generateOperation(del)
		}
	}
}
Example #2
0
func (g *Generator) generateHandlers(buf *bytes.Buffer, specDoc *spec.Document) {
	g.Buffer = buf
	paths := specDoc.AllPaths()
	groups := make(map[string]string)

	g.p("package ", specDoc.Spec().Info.Title)
	g.p()
	g.p()
	g.p("import (")
	g.p("	\"github.com/gin-gonic/gin\"")
	g.p(")")
	g.p()

	for _, path := range paths {
		operations := path.PathItemProps
		if post := operations.Post; post != nil {
			tag := post.OperationProps.Tags[0]
			groups[tag] = g.caps(tag)
		}
		if get := operations.Get; get != nil {
			tag := get.OperationProps.Tags[0]
			groups[tag] = g.caps(tag)
		}
		if put := operations.Put; put != nil {
			tag := put.OperationProps.Tags[0]
			groups[tag] = g.caps(tag)
		}
		if del := operations.Delete; del != nil {
			tag := del.OperationProps.Tags[0]
			groups[tag] = g.caps(tag)
		}
	}

	g.p("var (")
	for _, group := range groups {
		g.p(group, " *gin.RouterGroup")
	}
	g.p(")")
	g.p()
	g.p("func AddRoutes() {")

	for group, _ := range groups {
		for pname, path := range paths {
			operations := path.PathItemProps
			if post := operations.Post; post != nil {
				g.generateRouter("POST", group, pname, post)
			}
			if get := operations.Get; get != nil {
				g.generateRouter("GET", group, pname, get)
			}
			if put := operations.Put; put != nil {
				g.generateRouter("PUT", group, pname, put)
			}
			if del := operations.Delete; del != nil {
				g.generateRouter("DELETE", group, pname, del)
			}
		}
		g.p()
	}

	g.p("}")
	g.p()

	for group, _ := range groups {
		for _, path := range paths {
			operations := path.PathItemProps
			if post := operations.Post; post != nil {
				g.generateHandler(group, post)
			}
			if get := operations.Get; get != nil {
				g.generateHandler(group, get)
			}
			if put := operations.Put; put != nil {
				g.generateHandler(group, put)
			}
			if del := operations.Delete; del != nil {
				g.generateHandler(group, del)
			}
		}
	}
}