func (s *regSvc) GetServiceHosts(service string, version string) ([]common.ServiceHost, error) { var result, found, err = linq.From(s.definitions).FirstBy(s.getFilterByIdentifier(service)) if err != nil { return nil, err } if !found { return nil, fmt.Errorf("Cannot find a matching service definition: %s", service) } var serviceDef = result.(*common.ServiceDefinition) result, found, err = linq.From(serviceDef.Versions).FirstBy(s.getFilterVersionsByVersion(version)) if err != nil { return nil, err } if !found { return nil, fmt.Errorf("Cannot find a matching service version: %s", version) } var serviceVersion = result.(*common.ServiceVersion) var mapped = []common.ServiceHost{} for _, e := range serviceVersion.Hosts { mapped = append(mapped, *e) } return mapped, nil }
// CreateServiceVersion attaches a new version to an existing service func (gsr *GenericServiceRegistry) CreateServiceVersion(serviceIdentifier string, version string) (*common.ServiceVersion, error) { var filterByID = func(t linq.T) (bool, error) { return t.(*common.ServiceDefinition).Identifier == serviceIdentifier, nil } var versionByID = func(t linq.T) (bool, error) { return t.(common.ServiceVersion).Version == version, nil } var element, found, err = linq.From(gsr.services).FirstBy(filterByID) if !found { return nil, fmt.Errorf("Attempt at overriding service identifier: %s", serviceIdentifier) } else if err != nil { return nil, fmt.Errorf("Suffered error while filtering for service with identifier: %s", serviceIdentifier) } found, err = linq.From((element.(*common.ServiceDefinition)).ServiceVersions).AnyWith(versionByID) if found { return nil, fmt.Errorf("Attempt at overriding service %s version: %s", serviceIdentifier, version) } else if err != nil { return nil, fmt.Errorf("Suffered error while filtering for service version: %s", version) } var sv = common.ServiceVersion{ Version: version, ServiceIdentifier: serviceIdentifier, } var definition = (element.(*common.ServiceDefinition)) definition.ServiceVersions = append(definition.ServiceVersions, sv) return &sv, nil }
func (s *regSvc) AddServiceHost(service string, version string, uri string) (common.ServiceHost, error) { var result, found, err = linq.From(s.definitions).FirstBy(s.getFilterByIdentifier(service)) if err != nil { return common.ServiceHost{}, err } if !found { return common.ServiceHost{}, fmt.Errorf("Cannot find a matching service definition: %s", service) } var serviceDef = result.(*common.ServiceDefinition) result, found, err = linq.From(serviceDef.Versions).FirstBy(s.getFilterVersionsByVersion(version)) if err != nil { return common.ServiceHost{}, err } if !found { return common.ServiceHost{}, fmt.Errorf("Cannot find a matching service version: %s", version) } var serviceVersion = result.(*common.ServiceVersion) found, err = linq.From(serviceVersion.Hosts).AnyWith(s.hostWithURI(uri)) if err != nil { return common.ServiceHost{}, err } if found { return common.ServiceHost{}, fmt.Errorf("Cannot override service host with: %s", uri) } var newHost = common.ServiceHost{ URI: uri, } serviceVersion.Hosts = append(serviceVersion.Hosts, &newHost) return newHost, nil }
func main() { var rs []linq.T var ri int var err error s := []string{"A", "B", "C", "B"} ri, err = linq.From(s).Count() if err != nil { panic(err) } fmt.Println("res:", ri) rs, err = linq.From(s).Distinct().Results() if err != nil { panic(err) } fmt.Println("res:", rs) rs, err = linq.From(s).Reverse().Results() if err != nil { panic(err) } fmt.Println("res:", rs) rs, err = linq.From(s).Where(func(a linq.T) (bool, error) { return a.(string) == "A", nil }).Results() if err != nil { panic(err) } fmt.Println("res:", rs) }
func contains(prefixes []string, str string) bool { matchingPrefix := func(prefix linq.T) (bool, error) { return strings.HasPrefix(str, prefix.(string)), nil } result, _ := linq.From(prefixes).AnyWith(matchingPrefix) return result }
func buildTreeNodes(src T, r *TreeNode, prefix string) { results := queryChildren(linq.From(src), prefix) for _, it := range results { child := newTreeNode(it.(TreePath)) r.Nodes = append(r.Nodes, child) buildTreeNodes(src, child, it.(TreePath).GetUri()+".") } }
func (s *regSvc) AddServiceVersion(serviceIdentifier string, version string) error { var result, found, _ = linq.From(s.definitions).FirstBy(s.getFilterByIdentifier(serviceIdentifier)) if !found { return fmt.Errorf("Service definition with the identifier: %s not found", serviceIdentifier) } var definition = result.(*common.ServiceDefinition) found, _ = linq.From(definition.Versions).AnyWith(s.getFilterVersionsByVersion(version)) if found { return fmt.Errorf("Cannot override existing version %s for service %s.", version, serviceIdentifier) } var newVersion = new(common.ServiceVersion) newVersion.Version = version newVersion.Hosts = []*common.ServiceHost{} definition.Versions = append(definition.Versions, newVersion) return nil }
func (s *regSvc) getServiceByIdentifer(identifier string) (*common.ServiceDefinition, error) { var result, found, err = linq.From(s.definitions).FirstBy(s.getFilterByIdentifier(identifier)) if err != nil { return nil, err } else if !found { return nil, nil } var sd = result.(*common.ServiceDefinition) return sd, nil }
func (s *regSvc) GetAPIVersion(version string) (common.APIVersion, error) { var result, found, err = linq.From(s.apiVersions).FirstBy(s.getFilterAPIsByVersion(version)) if err != nil { return common.APIVersion{}, err } else if !found { return common.APIVersion{}, fmt.Errorf("Cannot find API version %s", version) } var apiVersion = result.(*common.APIVersion) return *apiVersion, nil }
func (s *regSvc) LinkServiceToAPI(serviceIdentifier string, version string, apiVersion string) error { var result, found, err = linq.From(s.apiVersions).FirstBy(s.getFilterAPIsByVersion(apiVersion)) if err != nil { return err } if !found { return fmt.Errorf("Cannot find a matching api version: %s", apiVersion) } var api = result.(*common.APIVersion) result, found, err = linq.From(s.definitions).FirstBy(s.getFilterByIdentifier(serviceIdentifier)) if err != nil { return err } if !found { return fmt.Errorf("Cannot find a matching service definition: %s", serviceIdentifier) } var serviceDef = result.(*common.ServiceDefinition) result, found, err = linq.From(serviceDef.Versions).FirstBy(s.getFilterVersionsByVersion(version)) if err != nil { return err } if !found { return fmt.Errorf("Cannot find a matching service version: %s", version) } var serviceVersion = result.(*common.ServiceVersion) found, err = linq.From(api.ServiceVersions).AnyWith(func(x linq.T) (bool, error) { return x.(*common.ServiceVersion) == serviceVersion, nil }) if err != nil { return err } if found { return fmt.Errorf("Cannot override existing ServiceVersion (%s, %s) for api version: %s", serviceIdentifier, version, apiVersion) } api.ServiceVersions = append(api.ServiceVersions, serviceVersion) return nil }
func (s *regSvc) GetServiceVersions(serviceIdentifier string) ([]common.ServiceVersion, error) { var result, found, _ = linq.From(s.definitions).FirstBy(s.getFilterByIdentifier(serviceIdentifier)) if !found { return nil, fmt.Errorf("Service definition with the identifier: %s not found", serviceIdentifier) } var definition = result.(*common.ServiceDefinition) var mappedVersions = []common.ServiceVersion{} for _, x := range definition.Versions { mappedVersions = append(mappedVersions, *x) } return mappedVersions, nil }
// CreateServiceDefinition create a new service definition entry func (gsr *GenericServiceRegistry) CreateServiceDefinition(identifierCode string, uriPrefix string, displayName string) (*common.ServiceDefinition, error) { var filterByID = func(t linq.T) (bool, error) { return t.(*common.ServiceDefinition).Identifier == identifierCode, nil } var filterByPrefix = func(t linq.T) (bool, error) { return t.(common.ServiceDefinition).URIPrefix == uriPrefix, nil } // Lock the service definitions slices write lock gsr.servicesMutex.Lock() // Try to find an existing service definition with the given identifier. If so - fail and return error. var found, err = linq.From(gsr.services).AnyWith(filterByID) if found { return nil, fmt.Errorf("Attempt at overriding service identifier: %s", identifierCode) } else if err != nil { return nil, fmt.Errorf("Suffered error while filtering for service with identifier: %s", identifierCode) } // Try to find an existing service definition with the given URI prefix. If so - fail and return error. found, err = linq.From(gsr.services).AnyWith(filterByPrefix) if found { return nil, fmt.Errorf("Attempt at overriding service URI prefix: %s", uriPrefix) } else if err != nil { return nil, fmt.Errorf("Suffered error while filtering for service with URI prefix: %s", uriPrefix) } var sd = common.ServiceDefinition{ Identifier: identifierCode, URIPrefix: uriPrefix, DisplayName: displayName, } gsr.services = append(gsr.services, &sd) // Unlock the service definitions slices write lock gsr.servicesMutex.Unlock() return &sd, nil }
func (s *regSvc) AddAPIVersion(version string, displayName string) (common.APIVersion, error) { var found, err = linq.From(s.apiVersions).AnyWith(s.getFilterAPIsByVersion(version)) if err != nil { return common.APIVersion{}, err } else if found { return common.APIVersion{}, fmt.Errorf("Cannot insert duplicate entry for api version %s", version) } var subject = common.APIVersion{ Version: version, Name: displayName, } s.apiVersions = append(s.apiVersions, &subject) return subject, nil }