func()

in api/internal/handler/data_loader/route_import.go [141:199]


func (h *ImportHandler) preCheck(ctx context.Context, data *loader.DataSets) map[store.HubKey][]string {
	errs := make(map[store.HubKey][]string)
	for _, route := range data.Routes {
		errs[store.HubKeyRoute] = make([]string, 0)
		o, err := h.routeStore.List(ctx, store.ListInput{
			// The check logic here is that if when a duplicate HOST or URI
			// has been found, the HTTP method is checked for overlap, and
			// if there is overlap it is determined to be a duplicate route
			// and the import is rejected.
			Predicate: func(obj interface{}) bool {
				r := obj.(*entity.Route)

				// Check URI and host duplication
				isURIDuplicated := r.URI != "" && route.URI != "" && r.URI == route.URI
				isURIsDuplicated := len(r.Uris) > 0 && len(route.Uris) > 0 &&
					len(intersect.Hash(r.Uris, route.Uris)) > 0
				isMethodDuplicated := len(intersect.Hash(r.Methods, route.Methods)) > 0

				// First check for duplicate URIs
				if isURIDuplicated || isURIsDuplicated {
					// Then check if the host field exists, and if it does, check for duplicates
					if r.Host != "" && route.Host != "" {
						return r.Host == route.Host && isMethodDuplicated
					} else if len(r.Hosts) > 0 && len(route.Hosts) > 0 {
						return len(intersect.Hash(r.Hosts, route.Hosts)) > 0 && isMethodDuplicated
					}
					// If the host field does not exist, only the presence or absence
					// of HTTP method duplication is returned by default.
					return isMethodDuplicated
				}
				return false
			},
			PageSize:   0,
			PageNumber: 0,
		})
		if err != nil {
			// When a special storage layer error occurs, return directly.
			return map[store.HubKey][]string{
				store.HubKeyRoute: {err.Error()},
			}
		}

		// Duplicate routes found
		if o.TotalSize > 0 {
			for _, row := range o.Rows {
				r, ok := row.(*entity.Route)
				if ok {
					errs[store.HubKeyRoute] = append(errs[store.HubKeyRoute],
						errors.Errorf("%s is duplicated with route %s",
							route.Uris[0],
							r.Name).
							Error())
				}
			}
		}
	}

	return errs
}