Skip to content

Commit

Permalink
cmd/definitions: Implement GSP-725 Add Defaultable Property for Pair (#…
Browse files Browse the repository at this point in the history
…730)

* cmd/definitions: Implement GSP-725 Add Defaultable Property for Pair

* Remove defaultable from namespace
  • Loading branch information
JinnyYi authored Aug 31, 2021
1 parent 46f8633 commit 1530ebb
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 71 deletions.
12 changes: 6 additions & 6 deletions cmd/definitions/bindata/bindata.go

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

19 changes: 10 additions & 9 deletions cmd/definitions/specs/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type tomlInfos map[string]tomlInfo

type tomlPair struct {
Type string `toml:"type"`
Defaultable bool `toml:"defaultable"`
Description string `toml:"description,optional"`
}

Expand Down Expand Up @@ -61,11 +62,10 @@ type tomlOp struct {
}

type tomlNamespace struct {
Features []string `toml:"features"`
Implement []string `toml:"implement"`
Defaultable []string `toml:"defaultable"`
New tomlOp `toml:"new"`
Op map[string]tomlOp `toml:"op"`
Features []string `toml:"features"`
Implement []string `toml:"implement"`
New tomlOp `toml:"new"`
Op map[string]tomlOp `toml:"op"`
}

type tomlService struct {
Expand Down Expand Up @@ -115,6 +115,7 @@ func parsePairs() Pairs {
p := Pair{
Name: k,
Type: v.Type,
Defaultable: v.Defaultable,
Description: v.Description,
}

Expand Down Expand Up @@ -242,6 +243,7 @@ func parseService(filePath string) Service {
ps.Pairs = append(ps.Pairs, Pair{
Name: k,
Type: v.Type,
Defaultable: v.Defaultable,
Description: v.Description,
})
}
Expand All @@ -265,10 +267,9 @@ func parseService(filePath string) Service {
// Parse namespace.
for name, v := range ts.Namespace {
n := Namespace{
Name: name,
Implement: v.Implement,
Features: v.Features,
Defaultable: v.Defaultable,
Name: name,
Implement: v.Implement,
Features: v.Features,
New: New{
Required: v.New.Required,
Optional: v.New.Optional,
Expand Down
12 changes: 6 additions & 6 deletions cmd/definitions/specs/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,17 +134,17 @@ func (p Pairs) Sort() {
type Pair struct {
Name string
Type string
Defaultable bool
Description string
}

// Namespace is the data parsed from TOML.
type Namespace struct {
Name string
Features []string // The feature names that provided by current namespace.
Implement []string
Defaultable []string // The defaultable pairs for the current namespace.
New New
Op []Op
Name string
Features []string // The feature names that provided by current namespace.
Implement []string
New New
Op []Op
}

// Sort will sort the Namespace
Expand Down
12 changes: 12 additions & 0 deletions cmd/definitions/tmpl/pair.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,16 @@ func With{{ $pname }}(v {{ $v.Type }}) Pair {
Value: v,
}
}

{{ if $v.Defaultable }}
// WithDefault{{ $pname }} will apply default_{{ $v.Name }} value to Options.
//
{{ $v.Description }}
func WithDefault{{ $pname }}(v {{ $v.Type }}) Pair {
return Pair{
Key: "default_{{ $v.Name }}",
Value: v,
}
}
{{- end }}
{{- end }}
87 changes: 48 additions & 39 deletions cmd/definitions/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,11 @@ func (s *Service) Pairs() []*Pair {

// Namespace contains all info about a namespace
type Namespace struct {
Name string
Features []*Feature
New *Function
Funcs []*Function
Interfaces []*Interface
defaultable map[string]*Pair
Name string
Features []*Feature
New *Function
Funcs []*Function
Interfaces []*Interface

HasFeatureLoosePair bool // Add a marker to support feature loose_pair
}
Expand All @@ -93,23 +92,23 @@ type PairFuncs struct {

// Defaultable returns sorted PairFuncs slice for defaultable pairs.
func (n *Namespace) Defaultable() []*PairFuncs {
pfs := make([]*PairFuncs, 0, len(n.defaultable))

for _, v := range n.defaultable {
var ops []string
for _, op := range n.Funcs {
ps := make([]*Pair, 0)
ps = append(ps, op.Required...)
ps = append(ps, op.Optional...)

for _, pair := range ps {
if pair.Name == v.Name {
ops = append(ops, op.Name)
break
}
pfm := make(map[*Pair][]string)
pfs := make([]*PairFuncs, 0)

for _, op := range n.Funcs {
ps := make([]*Pair, 0)
ps = append(ps, op.Required...)
ps = append(ps, op.Optional...)

for _, pair := range ps {
if pair.Defaultable {
pfm[pair] = append(pfm[pair], op.Name)
}
}
pf := &PairFuncs{Pair: v, Funcs: ops}
}

for pair, ops := range pfm {
pf := &PairFuncs{Pair: pair, Funcs: ops}
pfs = append(pfs, pf)
}

Expand Down Expand Up @@ -149,6 +148,8 @@ type Pair struct {

ptype string

Defaultable bool

// Runtime generated
Global bool
Description string
Expand All @@ -163,6 +164,7 @@ func (p *Pair) Format(s specs.Pair, global bool) {
p.Name = s.Name
p.ptype = s.Type
p.Global = global
p.Defaultable = s.Defaultable

p.Description = formatDescription(templateutils.ToPascal(p.Name), s.Description)
}
Expand Down Expand Up @@ -555,6 +557,7 @@ func (d *Data) FormatNamespace(srv *Service, n specs.Namespace) *Namespace {
Name: name,
ptype: "bool",
Global: false,
Defaultable: false,
Description: f.Description,
}
srv.pairs[featurePair.Name] = featurePair
Expand Down Expand Up @@ -600,24 +603,6 @@ func (d *Data) FormatNamespace(srv *Service, n specs.Namespace) *Namespace {
}
}

// Handle defaultable pairs.
ns.defaultable = make(map[string]*Pair)
for _, defaultablePairName := range n.Defaultable {
pair, ok := srv.pairs[defaultablePairName]
if !ok {
log.Fatalf("invalid defaultable pair: %s", defaultablePairName)
}
name := fmt.Sprintf("default_%s", defaultablePairName)
defaultPair := &Pair{
Name: name,
ptype: pair.ptype,
Global: false,
Description: pair.Description,
}
srv.pairs[name] = defaultPair
ns.defaultable[defaultablePairName] = pair
}

d.ValidateNamespace(srv, ns)
return ns
}
Expand Down Expand Up @@ -706,13 +691,37 @@ func mergePairs(global, service map[string]*Pair) map[string]*Pair {
for k, v := range global {
v := v
ans[k] = v
// Handle global defaultable pairs.
if v.Defaultable {
name := fmt.Sprintf("default_%s", v.Name)
pair := &Pair{
Name: name,
ptype: v.ptype,
Global: true,
Defaultable: false,
Description: v.Description,
}
ans[name] = pair
}
}
for k, v := range service {
if _, ok := ans[k]; ok {
log.Fatalf("pair conflict: %s", k)
}
v := v
ans[k] = v
// Handle system defaultable pairs.
if v.Defaultable {
name := fmt.Sprintf("default_%s", v.Name)
pair := &Pair{
Name: name,
ptype: v.ptype,
Global: false,
Defaultable: false,
Description: v.Description,
}
ans[name] = pair
}
}
return ans
}
Expand Down
2 changes: 2 additions & 0 deletions definitions/pairs.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
type = "string"

[content_type]
defaultable = true
type = "string"

[continuation_token]
Expand Down Expand Up @@ -47,6 +48,7 @@ type = "string"

[io_callback]
type = "func([]byte)"
defaultable = true
description = "specify what todo every time we read data from source"

[size]
Expand Down
20 changes: 20 additions & 0 deletions pairs/generated.go

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

24 changes: 14 additions & 10 deletions tests/generated.go

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

Loading

0 comments on commit 1530ebb

Please sign in to comment.