From 16ac41b3b919f057bcdbb48854c98646313d3262 Mon Sep 17 00:00:00 2001 From: JinnyYi Date: Wed, 22 Sep 2021 16:33:29 +0800 Subject: [PATCH 1/4] fix: Compilation fails if Default*Pairs unimplemented in service --- cmd/definitions/gen_service.go | 25 +++++++++++++++++++++-- cmd/definitions/type.go | 11 ++--------- tests/generated.go | 36 +++++++++++----------------------- 3 files changed, 36 insertions(+), 36 deletions(-) diff --git a/cmd/definitions/gen_service.go b/cmd/definitions/gen_service.go index d7af59cd2..ff09bfd3f 100644 --- a/cmd/definitions/gen_service.go +++ b/cmd/definitions/gen_service.go @@ -193,20 +193,39 @@ GetStorageSystemMetadata will get StorageSystemMetadata from Storage. AddField("pairs", "[]Pair"). AddLine() + var needDefaultPairs = true + defaultPairs := fmt.Sprintf("Default%sPairs", nsNameP) // Generate required pairs. pairStruct.AddLineComment("Required pairs") for _, pair := range ns.New.ParsedRequired() { pairNameP := templateutils.ToPascal(pair.Name) + if pairNameP == defaultPairs { + needDefaultPairs = false + } pairStruct.AddField("Has"+pairNameP, "bool") pairStruct.AddField(pairNameP, pair.Type) + if pair.Defaultable { + needDefaultPairs = true + } } // Generate optional pairs. pairStruct.AddLineComment("Optional pairs") for _, pair := range ns.New.ParsedOptional() { pairNameP := templateutils.ToPascal(pair.Name) + if pairNameP == defaultPairs { + needDefaultPairs = false + } pairStruct.AddField("Has"+pairNameP, "bool") pairStruct.AddField(pairNameP, pair.Type) + if pair.Defaultable { + needDefaultPairs = true + } + } + + if needDefaultPairs { + pairStruct.AddField("HasDefault"+nsNameP+"Pairs", "bool") + pairStruct.AddField("Default"+nsNameP+"Pairs", "Default"+nsNameP+"Pairs") } // Generate feature handle logic. @@ -268,7 +287,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), @@ -276,8 +296,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..324e56e03 100644 --- a/cmd/definitions/type.go +++ b/cmd/definitions/type.go @@ -307,15 +307,8 @@ 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 srv.Pairs { - 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) } // Handle other functions. 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)) From b9aba8b937a47768341de74b443104cd7d558ac4 Mon Sep 17 00:00:00 2001 From: JinnyYi Date: Wed, 22 Sep 2021 18:16:56 +0800 Subject: [PATCH 2/4] rollback changes for default*Pairs --- cmd/definitions/gen_service.go | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/cmd/definitions/gen_service.go b/cmd/definitions/gen_service.go index ff09bfd3f..7b0c6c267 100644 --- a/cmd/definitions/gen_service.go +++ b/cmd/definitions/gen_service.go @@ -193,39 +193,20 @@ GetStorageSystemMetadata will get StorageSystemMetadata from Storage. AddField("pairs", "[]Pair"). AddLine() - var needDefaultPairs = true - defaultPairs := fmt.Sprintf("Default%sPairs", nsNameP) // Generate required pairs. pairStruct.AddLineComment("Required pairs") for _, pair := range ns.New.ParsedRequired() { pairNameP := templateutils.ToPascal(pair.Name) - if pairNameP == defaultPairs { - needDefaultPairs = false - } pairStruct.AddField("Has"+pairNameP, "bool") pairStruct.AddField(pairNameP, pair.Type) - if pair.Defaultable { - needDefaultPairs = true - } } // Generate optional pairs. pairStruct.AddLineComment("Optional pairs") for _, pair := range ns.New.ParsedOptional() { pairNameP := templateutils.ToPascal(pair.Name) - if pairNameP == defaultPairs { - needDefaultPairs = false - } pairStruct.AddField("Has"+pairNameP, "bool") pairStruct.AddField(pairNameP, pair.Type) - if pair.Defaultable { - needDefaultPairs = true - } - } - - if needDefaultPairs { - pairStruct.AddField("HasDefault"+nsNameP+"Pairs", "bool") - pairStruct.AddField("Default"+nsNameP+"Pairs", "Default"+nsNameP+"Pairs") } // Generate feature handle logic. From 449ffc18c0d12e9bdbbdc4705a4ec023e0898aca Mon Sep 17 00:00:00 2001 From: JinnyYi Date: Thu, 23 Sep 2021 16:28:00 +0800 Subject: [PATCH 3/4] Add default_*_pair and *_feature by default --- cmd/definitions/type.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/cmd/definitions/type.go b/cmd/definitions/type.go index 324e56e03..06d76efd3 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) @@ -310,6 +311,33 @@ func (d *Data) LoadService(filePath string) { 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 { + hasDefaultNsPairs = true + } + if v == nsFeaturesName { + hasNsFeatures = true + } + } + if !hasDefaultNsPairs { + ns.New.Optional = append(ns.New.Optional, defaultNsPairsName) + srv.Pairs[defaultNsPairsName] = &Pair{ + Name: defaultNsPairsName, + Type: templateutils.ToPascal(defaultNsPairsName), + } + } + if !hasNsFeatures { + ns.New.Optional = append(ns.New.Optional, nsFeaturesName) + srv.Pairs[nsFeaturesName] = &Pair{ + Name: nsFeaturesName, + Type: templateutils.ToPascal(nsFeaturesName), + } + } // Handle other functions. for k, v := range ns.Op { From 2653a9b87539a88edd16f67f1683854824d4cb51 Mon Sep 17 00:00:00 2001 From: JinnyYi Date: Fri, 24 Sep 2021 17:14:14 +0800 Subject: [PATCH 4/4] Add warning message --- cmd/definitions/type.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cmd/definitions/type.go b/cmd/definitions/type.go index 06d76efd3..254c28f4e 100644 --- a/cmd/definitions/type.go +++ b/cmd/definitions/type.go @@ -318,9 +318,11 @@ func (d *Data) LoadService(filePath string) { 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 } }