diff --git a/cmd/definitions/gen_service.go b/cmd/definitions/gen_service.go index 918a531c0..fcd89e236 100644 --- a/cmd/definitions/gen_service.go +++ b/cmd/definitions/gen_service.go @@ -270,7 +270,8 @@ GetStorageSystemMetadata will get StorageSystemMetadata from Storage. for _, feature := range ns.ParsedFeatures() { featureNameP := templateutils.ToPascal(feature.Name) - gg.If(gg.S("result.hasEnable%s", featureNameP)). + group. + NewIf(gg.S("result.hasEnable%s", featureNameP)). AddBody( gg.S("result.Has%sFeatures = true", nsNameP), gg.S("result.%sFeatures.%s = true", nsNameP, featureNameP), @@ -278,8 +279,9 @@ GetStorageSystemMetadata will get StorageSystemMetadata from Storage. } return group }), + gg.LineComment("Default pairs"), gg.Embed(func() gg.Node { - // Generate default pari handle logic here. + // Generate default pair handle logic here. group := gg.NewGroup() for _, dp := range ns.ParsedDefaultable() { diff --git a/cmd/definitions/type.go b/cmd/definitions/type.go index 5738ec42c..254c28f4e 100644 --- a/cmd/definitions/type.go +++ b/cmd/definitions/type.go @@ -236,6 +236,7 @@ func (d *Data) LoadService(filePath string) { d.Service = &Service{ d: d, } + d.Service.Pairs = make(map[string]*Pair) err = parseTOML(bs, d.Service) if err != nil { log.Fatalf("parse service: %v", err) @@ -307,14 +308,36 @@ func (d *Data) LoadService(filePath string) { ns.New.srv = srv ns.New.ns = ns ns.New.Implemented = true - for _, v := range d.PairsMap { - if v.Defaultable { - ns.New.Optional = append(ns.New.Optional, "default_"+v.Name) + for _, v := range ns.ParsedDefaultable() { + ns.New.Optional = append(ns.New.Optional, "default_"+v.Pair.Name) + } + // add default_*_pairs and *_features + hasDefaultNsPairs := false + hasNsFeatures := false + defaultNsPairsName := fmt.Sprintf("default_%s_pairs", ns.Name) + nsFeaturesName := fmt.Sprintf("%s_features", ns.Name) + for _, v := range ns.New.Optional { + if v == defaultNsPairsName { + log.Warnf("Please remove %s pair as it will be automatically generated.", defaultNsPairsName) + hasDefaultNsPairs = true + } + if v == nsFeaturesName { + log.Warnf("Please remove %s pair as it will be automatically generated.", nsFeaturesName) + hasNsFeatures = true + } + } + if !hasDefaultNsPairs { + ns.New.Optional = append(ns.New.Optional, defaultNsPairsName) + srv.Pairs[defaultNsPairsName] = &Pair{ + Name: defaultNsPairsName, + Type: templateutils.ToPascal(defaultNsPairsName), } } - for _, v := range srv.Pairs { - if v.Defaultable { - ns.New.Optional = append(ns.New.Optional, "default_"+v.Name) + if !hasNsFeatures { + ns.New.Optional = append(ns.New.Optional, nsFeaturesName) + srv.Pairs[nsFeaturesName] = &Pair{ + Name: nsFeaturesName, + Type: templateutils.ToPascal(nsFeaturesName), } } diff --git a/tests/generated.go b/tests/generated.go index b5b038daa..f344c6fa4 100644 --- a/tests/generated.go +++ b/tests/generated.go @@ -163,14 +163,8 @@ type pairServiceNew struct { HasCredential bool Credential string // Optional pairs - HasDefaultContentType bool - DefaultContentType string - HasDefaultIoCallback bool - DefaultIoCallback func([]byte) HasDefaultServicePairs bool DefaultServicePairs DefaultServicePairs - HasDefaultStorageClass bool - DefaultStorageClass string HasEndpoint bool Endpoint string HasHTTPClientOptions bool @@ -193,30 +187,12 @@ func parsePairServiceNew(opts []Pair) (pairServiceNew, error) { } result.HasCredential = true result.Credential = v.Value.(string) - case "default_content_type": - if result.HasDefaultContentType { - continue - } - result.HasDefaultContentType = true - result.DefaultContentType = v.Value.(string) - case "default_io_callback": - if result.HasDefaultIoCallback { - continue - } - result.HasDefaultIoCallback = true - result.DefaultIoCallback = v.Value.(func([]byte)) case "default_service_pairs": if result.HasDefaultServicePairs { continue } result.HasDefaultServicePairs = true result.DefaultServicePairs = v.Value.(DefaultServicePairs) - case "default_storage_class": - if result.HasDefaultStorageClass { - continue - } - result.HasDefaultStorageClass = true - result.DefaultStorageClass = v.Value.(string) case "endpoint": if result.HasEndpoint { continue @@ -239,6 +215,8 @@ func parsePairServiceNew(opts []Pair) (pairServiceNew, error) { } // Enable features + // Default pairs + if !result.HasCredential { return pairServiceNew{}, services.PairRequiredError{Keys: []string{"credential"}} } @@ -585,7 +563,15 @@ func parsePairStorageNew(opts []Pair) (pairStorageNew, error) { } } // Enable features - + if result.hasEnableLoosePair { + result.HasStorageFeatures = true + result.StorageFeatures.LoosePair = true + } + if result.hasEnableVirtualDir { + result.HasStorageFeatures = true + result.StorageFeatures.VirtualDir = true + } + // Default pairs if result.HasDefaultContentType { result.HasDefaultStoragePairs = true result.DefaultStoragePairs.Write = append(result.DefaultStoragePairs.Write, WithContentType(result.DefaultContentType))