Skip to content

Commit

Permalink
cmd/definitions: Trim Service Pair Prefix (#598)
Browse files Browse the repository at this point in the history
* - Trim Service Pair Prefix
- Use logrus to log a warning

* remove FullName
  • Loading branch information
xxchan authored Jun 15, 2021
1 parent 2d76235 commit 72ad65e
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 43 deletions.
6 changes: 3 additions & 3 deletions cmd/definitions/bindata.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion cmd/definitions/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import (
"fmt"
"go/parser"
"go/token"
"log"
"os"
"sort"
"text/template"

"github.com/Xuanwo/templateutils"
"github.com/dave/dst"
"github.com/dave/dst/decorator"
log "github.com/sirupsen/logrus"
)

var (
Expand Down
2 changes: 1 addition & 1 deletion cmd/definitions/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
package main

import (
"log"
"os"

specs "github.com/beyondstorage/specs/go"
log "github.com/sirupsen/logrus"
)

func main() {
Expand Down
2 changes: 1 addition & 1 deletion cmd/definitions/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ package main
import (
"fmt"
"io/ioutil"
"log"
"os"

"github.com/Xuanwo/templateutils"
specs "github.com/beyondstorage/specs/go"
log "github.com/sirupsen/logrus"
)

func parse() (data *Data) {
Expand Down
6 changes: 5 additions & 1 deletion cmd/definitions/tests/service.toml
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,8 @@ description = "set storage features"

[infos.object.meta.storage-class]
type = "string"
description = "is the storage class for this object"
description = "is the storage class for this object"

[pairs.size]
type = "int64"
description = "tests pair conflict"
30 changes: 12 additions & 18 deletions cmd/definitions/tmpl/service.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,6 @@ var _ httpclient.Options
// Type is the type for {{ .Name }}
const Type = "{{ .Name }}"

// Service available pairs.
const (
{{- range $_, $v := .Pairs }}
{{- if not $v.Global }}
{{ $v.Description }}
{{ $v.FullName }} = "{{ $.Name }}_{{ $v.Name }}"
{{- end }}
{{- end }}
)

// ObjectMetadata stores service metadata for object.
type ObjectMetadata struct {
{{- range $_, $v := .Infos }}
Expand Down Expand Up @@ -67,12 +57,16 @@ func setObjectMetadata(o *Object, om ObjectMetadata) {
{{- range $_, $v := .Pairs }}
{{- if not $v.Global }}
{{- $pname := $v.Name | toPascal }}
{{- if $v.Conflict }}
// Deprecated: Use pairs.With{{ $pname }} instead.
//
{{- end }}
// With{{ $pname }} will apply {{ $v.Name }} value to Options.
//
{{ $v.Description }}
func With{{ $pname }}(v {{ $v.Type }}) Pair {
return Pair{
Key: {{ $v.FullName }},
Key: "{{ $v.Name }}",
Value: v,
}
}
Expand Down Expand Up @@ -158,7 +152,7 @@ func parsePair{{ $pn }}{{ $fnk }}(opts []Pair) (pair{{ $pn }}{{ $fnk }}, error)
switch v.Key {
// Required pairs
{{- range $_, $pair := $fn.Required }}
case {{ $pair.FullName }}:
case "{{ $pair.Name }}":
if result.Has{{ $pair.Name | toPascal }} {
continue
}
Expand All @@ -167,7 +161,7 @@ func parsePair{{ $pn }}{{ $fnk }}(opts []Pair) (pair{{ $pn }}{{ $fnk }}, error)
{{- end }}
// Optional pairs
{{- range $_, $pair := $fn.Optional }}
case {{ $pair.FullName }}:
case "{{ $pair.Name }}":
if result.Has{{ $pair.Name | toPascal }} {
continue
}
Expand All @@ -179,7 +173,7 @@ func parsePair{{ $pn }}{{ $fnk }}(opts []Pair) (pair{{ $pn }}{{ $fnk }}, error)

{{- range $_, $pair := $fn.Required }}
if !result.Has{{ $pair.Name | toPascal }} {
return pair{{ $pn }}{{ $fnk }}{}, services.PairRequiredError{ Keys:[]string{ {{- $pair.FullName -}} } }
return pair{{ $pn }}{{ $fnk }}{}, services.PairRequiredError{ Keys:[]string{ "{{ $pair.Name }}" } }
}
{{- end }}

Expand Down Expand Up @@ -239,7 +233,7 @@ func (s *{{ $pn }})parsePair{{ $pn }}{{ $fnk }}(opts []Pair) (pair{{ $pn }}{{ $f

switch v.Key {
{{- range $_, $pair := $fn.Required }}
case {{ $pair.FullName }}:
case "{{ $pair.Name }}":
if result.Has{{ $pair.Name | toPascal }} {
continue
}
Expand All @@ -248,7 +242,7 @@ func (s *{{ $pn }})parsePair{{ $pn }}{{ $fnk }}(opts []Pair) (pair{{ $pn }}{{ $f
continue
{{- end }}
{{- range $_, $pair := $fn.Optional }}
case {{ $pair.FullName }}:
case "{{ $pair.Name }}":
if result.Has{{ $pair.Name | toPascal }} {
continue
}
Expand All @@ -257,7 +251,7 @@ func (s *{{ $pn }})parsePair{{ $pn }}{{ $fnk }}(opts []Pair) (pair{{ $pn }}{{ $f
continue
{{- end }}
{{- range $_, $pair := $fn.Virtual }}
case {{ $pair.FullName }}:
case "{{ $pair.Name }}":
if result.Has{{ $pair.Name | toPascal }} {
continue
}
Expand Down Expand Up @@ -287,7 +281,7 @@ func (s *{{ $pn }})parsePair{{ $pn }}{{ $fnk }}(opts []Pair) (pair{{ $pn }}{{ $f
// Check required pairs.
{{- range $_, $pair := $fn.Required }}
if !result.Has{{ $pair.Name | toPascal }} {
return pair{{ $pn }}{{ $fnk }}{}, services.PairRequiredError{ Keys:[]string{ {{- $pair.FullName -}} } }
return pair{{ $pn }}{{ $fnk }}{}, services.PairRequiredError{ Keys:[]string{ "{{ $pair.Name }}" } }
}
{{- end }}

Expand Down
34 changes: 18 additions & 16 deletions cmd/definitions/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ package main

import (
"fmt"
"log"
"sort"
"strings"

"github.com/Xuanwo/templateutils"
specs "github.com/beyondstorage/specs/go"
log "github.com/sirupsen/logrus"
)

// Data is the biggest container for all definitions.
Expand Down Expand Up @@ -98,6 +98,9 @@ type Pair struct {
// Runtime generated
Global bool
Description string

// This is a service pair having the same name and type as a global pair
Conflict bool
}

func (p *Pair) Type() string {
Expand All @@ -113,14 +116,6 @@ func (p *Pair) Format(s specs.Pair, global bool) {
p.Description = formatDescription(templateutils.ToPascal(p.Name), s.Description)
}

// FullName will print full name for current pair
func (p *Pair) FullName() string {
if p.Global {
return fmt.Sprintf("\"%s\"", p.Name)
}
return "pair" + templateutils.ToPascal(p.Name)
}

// Info is the metadata definition.
type Info struct {
Scope string
Expand Down Expand Up @@ -604,16 +599,23 @@ func FormatData(p specs.Pairs, m specs.Infos, o specs.Operations) *Data {
return data
}

func mergePairs(ms ...map[string]*Pair) map[string]*Pair {
func mergePairs(global, service map[string]*Pair) map[string]*Pair {
ans := make(map[string]*Pair)
for _, m := range ms {
for k, v := range m {
if _, ok := ans[k]; ok {
log.Fatalf("pair conflict: %s", k)
for k, v := range global {
v := v
ans[k] = v
}
for k, v := range service {
if p, ok := ans[k]; ok {
v.Conflict = true
if v.ptype == p.ptype {
log.Warnf("pair conflict: %s", k)
} else {
log.Fatalf("pair (%s, %s) conflicts with global pair (%s, %s)", k, v.ptype, k, p.ptype)
}
v := v
ans[k] = v
}
v := v
ans[k] = v
}
return ans
}
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ require (
github.com/golang/mock v1.5.0
github.com/google/uuid v1.2.0
github.com/kevinburke/go-bindata v3.22.0+incompatible
github.com/sirupsen/logrus v1.8.1
github.com/stretchr/testify v1.7.0
)
8 changes: 7 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ github.com/dave/gopackages v0.0.0-20170318123100-46e7023ec56e/go.mod h1:i00+b/gK
github.com/dave/jennifer v1.2.0/go.mod h1:fIb+770HOpJ2fmN9EPPKOqm1vMGhB+TwXKMZhrIygKg=
github.com/dave/kerr v0.0.0-20170318121727-bc25dd6abe8e/go.mod h1:qZqlPyPvfsDJt+3wHJ1EvSXDuVjFTK0j2p/ca+gtsb8=
github.com/dave/rebecca v0.9.1/go.mod h1:N6XYdMD/OKw3lkF3ywh8Z6wPGuwNFDNtWYEMFWEmXBA=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang/mock v1.5.0 h1:jlYHihg//f7RRwuPfptm04yp4s7O6Kw8EZiVYIGcH0g=
github.com/golang/mock v1.5.0/go.mod h1:CWnOUgYIOo4TcNZ0wHX3YZCqsaM1I1Jvs6v3mP3KVu8=
github.com/google/pprof v0.0.0-20181127221834-b4f47329b966/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
Expand All @@ -29,7 +30,10 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand All @@ -47,6 +51,8 @@ golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sys v0.0.0-20180903190138-2b024373dcd9/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20200509030707-2212a7e161a5 h1:MeC2gMlMdkd67dn17MEby3rGXRxZtWeiRXOnISfTQ74=
Expand Down
3 changes: 2 additions & 1 deletion internal/cmd/iterator/main.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 72ad65e

Please sign in to comment.