Пример #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
}
Пример #2
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)
		}
	}
}
Пример #3
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)
}
Пример #4
0
func gatherOperations(specDoc *spec.Document, operationIDs []string) map[string]spec.Operation {
	operations := make(map[string]spec.Operation)
	if len(operationIDs) == 0 {
		for _, k := range specDoc.OperationIDs() {
			if op, ok := specDoc.OperationForName(k); ok {
				operations[k] = *op
			}
		}
	} else {
		for _, k := range specDoc.OperationIDs() {
			for _, nm := range operationIDs {
				if k == nm {
					if op, ok := specDoc.OperationForName(k); ok {
						operations[k] = *op
					}
				}
			}
		}
	}
	return operations
}
Пример #5
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)
			}
		}
	}
}