Beispiel #1
0
func gatherModels(specDoc *spec.Document, modelNames []string) map[string]spec.Schema {
	models, mnc := make(map[string]spec.Schema), len(modelNames)
	for k, v := range specDoc.Spec().Definitions {
		for _, nm := range modelNames {
			if mnc == 0 || k == nm {
				models[k] = v
			}
		}
	}
	return models
}
Beispiel #2
0
func appNameOrDefault(specDoc *spec.Document, name, defaultName string) string {
	if name == "" {
		if specDoc.Spec().Info != nil && specDoc.Spec().Info.Title != "" {
			name = specDoc.Spec().Info.Title
		} else {
			name = defaultName
		}
	}
	return swag.ToGoName(name)
}
Beispiel #3
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)
			}
		}
	}
}