func()

in api/internal/handler/data_loader/loader/openapi3/import.go [66:121]


func (o Loader) convertToEntities(s *openapi3.Swagger) (*loader.DataSets, error) {
	var (
		// temporarily save the parsed data
		data = &loader.DataSets{}
		// global upstream ID
		globalUpstreamID = o.TaskName
		// global uri prefix
		globalPath = ""
	)

	// create upstream when servers field not empty
	if len(s.Servers) > 0 {
		upstream := entity.Upstream{
			BaseInfo: entity.BaseInfo{ID: globalUpstreamID},
			UpstreamDef: entity.UpstreamDef{
				Name: globalUpstreamID,
				Type: "roundrobin",
				Nodes: map[string]float64{
					"0.0.0.0": 1,
				},
			},
		}
		data.Upstreams = append(data.Upstreams, upstream)
	}

	// each one will correspond to a route
	for uri, v := range s.Paths {
		// replace parameter in uri to wildcard
		realUri := regURIVar.ReplaceAllString(uri, "*")
		// generate route Name
		routeName := o.TaskName + "_" + strings.TrimPrefix(uri, "/")

		// decide whether to merge multi-method routes based on configuration
		if o.MergeMethod {
			// create a single route for each path, merge all methods
			route := generateBaseRoute(routeName, v.Summary)
			route.Uris = []string{globalPath + realUri}
			route.UpstreamID = globalUpstreamID
			for method := range v.Operations() {
				route.Methods = append(route.Methods, strings.ToUpper(method))
			}
			data.Routes = append(data.Routes, route)
		} else {
			// create routes for each method of each path
			for method, operation := range v.Operations() {
				subRouteID := routeName + "_" + method
				route := generateBaseRoute(subRouteID, operation.Summary)
				route.Uris = []string{globalPath + realUri}
				route.Methods = []string{strings.ToUpper(method)}
				route.UpstreamID = globalUpstreamID
				data.Routes = append(data.Routes, route)
			}
		}
	}
	return data, nil
}