Skip to content

Commit

Permalink
new: support default order at spec level
Browse files Browse the repository at this point in the history
  • Loading branch information
primalmotion committed Sep 24, 2019
1 parent 4fad13a commit 9561ebf
Show file tree
Hide file tree
Showing 13 changed files with 93 additions and 106 deletions.
2 changes: 1 addition & 1 deletion cmd/rego/static/bindata.go

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

22 changes: 11 additions & 11 deletions schema/bindata.go

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions schema/rego-abstract.in
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
},
"indexes": {
"$ref": "#/definitions/indexes"
},
"default_order": {
"$ref": "#/definitions/default_order"
}
},
"definitions": {
Expand All @@ -25,6 +28,13 @@
"type": "string"
}
}
},
"default_order": {
"description": "Attribute that should be used to order results if not manually provided",
"type": "array",
"items": {
"type": "string"
}
},
"attributes": {
__ATTRIBUTE__
Expand Down
14 changes: 10 additions & 4 deletions schema/rego-abstract.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
},
"indexes": {
"$ref": "#/definitions/indexes"
},
"default_order": {
"$ref": "#/definitions/default_order"
}
},
"definitions": {
Expand All @@ -25,6 +28,13 @@
"type": "string"
}
}
},
"default_order": {
"description": "Attribute that should be used to order results if not manually provided",
"type": "array",
"items": {
"type": "string"
}
},
"attributes": {
"type": "object",
Expand Down Expand Up @@ -68,10 +78,6 @@
"description": "The attribute can only be set during create operation",
"type": "boolean"
},
"default_order": {
"description": "The value of the attribute is used as default ordering key",
"type": "boolean"
},
"default_value": {
"description": "The default value of the attribute",
"type": ["array", "boolean", "integer", "null", "number", "object", "string"]
Expand Down
4 changes: 0 additions & 4 deletions schema/rego-attribute.in
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@
"description": "The attribute can only be set during create operation",
"type": "boolean"
},
"default_order": {
"description": "The value of the attribute is used as default ordering key",
"type": "boolean"
},
"default_value": {
"description": "The default value of the attribute",
"type": ["array", "boolean", "integer", "null", "number", "object", "string"]
Expand Down
10 changes: 10 additions & 0 deletions schema/rego-spec.in
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
},
"indexes": {
"$ref": "#/definitions/indexes"
},
"default_order": {
"$ref": "#/definitions/default_order"
}
},
"definitions": {
Expand All @@ -38,6 +41,13 @@
}
}
},
"default_order": {
"description": "Attribute that should be used to order results if not manually provided",
"type": "array",
"items": {
"type": "string"
}
},
"model": {
"title": "Model",
"description": "Holds basic information of the represented object.",
Expand Down
14 changes: 10 additions & 4 deletions schema/rego-spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
},
"indexes": {
"$ref": "#/definitions/indexes"
},
"default_order": {
"$ref": "#/definitions/default_order"
}
},
"definitions": {
Expand All @@ -38,6 +41,13 @@
}
}
},
"default_order": {
"description": "Attribute that should be used to order results if not manually provided",
"type": "array",
"items": {
"type": "string"
}
},
"model": {
"title": "Model",
"description": "Holds basic information of the represented object.",
Expand Down Expand Up @@ -256,10 +266,6 @@
"description": "The attribute can only be set during create operation",
"type": "boolean"
},
"default_order": {
"description": "The value of the attribute is used as default ordering key",
"type": "boolean"
},
"default_value": {
"description": "The default value of the attribute",
"type": ["array", "boolean", "integer", "null", "number", "object", "string"]
Expand Down
1 change: 0 additions & 1 deletion spec/attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ type Attribute struct {
AllowedCharsMessage string `yaml:"allowed_chars_message,omitempty" json:"allowed_chars_message,omitempty"`
AllowedChoices []string `yaml:"allowed_choices,omitempty" json:"allowed_choices,omitempty"`
Autogenerated bool `yaml:"autogenerated,omitempty" json:"autogenerated,omitempty"`
DefaultOrder bool `yaml:"default_order,omitempty" json:"default_order,omitempty"`
DefaultValue interface{} `yaml:"default_value,omitempty" json:"default_value,omitempty"`
Deprecated bool `yaml:"deprecated,omitempty" json:"deprecated,omitempty"`
ExampleValue interface{} `yaml:"example_value,omitempty" json:"example_value,omitempty"`
Expand Down
4 changes: 2 additions & 2 deletions spec/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ type Specification interface {
// ExposedAttributes returns only the exposed attributes in the given version.
ExposedAttributes(version string) []*Attribute

// OrderingAttributes returns the list of attributes used for ordering.
OrderingAttributes(version string) []*Attribute
// DefaultOrder returns the default ordering of the spec.
DefaultOrder() []string

// AttributeVersions returns all the versions of attributes.
AttributeVersions() []string
Expand Down
5 changes: 5 additions & 0 deletions spec/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,18 +244,23 @@ func LoadSpecificationSet(
for _, spec := range set.specs {

// Apply base specs.
var ordering []string
for _, ext := range spec.Model().Extends {

base, ok := baseSpecs[ext]
if !ok {
return nil, fmt.Errorf("unable to find base spec '%s' for spec '%s'", ext, spec.Model().RestName)
}

ordering = append(ordering, base.DefaultOrder()...)

if err = spec.ApplyBaseSpecifications(base); err != nil {
return nil, err
}
}

spec.(*specification).RawDefaultOrder = append(ordering, spec.(*specification).RawDefaultOrder...)

// Link the APIs to corresponding specifications
for _, rel := range spec.Relations() {

Expand Down
71 changes: 34 additions & 37 deletions spec/specification.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,28 @@ import (
)

const (
rootModelKey = "model"
rootIndexesKey = "indexes"
rootAttributesKey = "attributes"
rootRelationsKey = "relations"
rootModelKey = "model"
rootIndexesKey = "indexes"
rootDefaultOrderKey = "default_order"
rootAttributesKey = "attributes"
rootRelationsKey = "relations"
)

type versionedAttributes map[string][]*Attribute
type attributeMapping map[string]map[string]*Attribute
type relationMapping map[string]*Relation

type specification struct {
RawAttributes versionedAttributes `yaml:"attributes,omitempty" json:"attributes,omitempty"`
RawRelations []*Relation `yaml:"relations,omitempty" json:"relations,omitempty"`
RawModel *Model `yaml:"model,omitempty" json:"model,omitempty"`
RawIndexes [][]string `yaml:"indexes,omitempty" json:"indexes,omitempty"`

attributeMap attributeMapping
relationsMap relationMapping
orderingAttributes versionedAttributes
identifier *Attribute
path string
RawAttributes versionedAttributes `yaml:"attributes,omitempty" json:"attributes,omitempty"`
RawRelations []*Relation `yaml:"relations,omitempty" json:"relations,omitempty"`
RawModel *Model `yaml:"model,omitempty" json:"model,omitempty"`
RawIndexes [][]string `yaml:"indexes,omitempty" json:"indexes,omitempty"`
RawDefaultOrder []string `yaml:"default_order,omitempty" json:"default_order,omitempty"`

attributeMap attributeMapping
relationsMap relationMapping
identifier *Attribute
path string
}

// NewSpecification returns a new specification.
Expand Down Expand Up @@ -153,6 +154,10 @@ func (s *specification) Write(writer io.Writer) error {
repr = append(repr, yaml.MapItem{Key: rootModelKey, Value: toYAMLMapSlice(s.RawModel)})
}

if len(s.RawDefaultOrder) != 0 {
repr = append(repr, yaml.MapItem{Key: rootDefaultOrderKey, Value: s.RawDefaultOrder})
}

if len(s.RawIndexes) != 0 {
repr = append(repr, yaml.MapItem{Key: rootIndexesKey, Value: s.RawIndexes})
}
Expand Down Expand Up @@ -238,6 +243,7 @@ func (s *specification) Write(writer io.Writer) error {
prfx4 := []byte(" - name")
sufx1 := []byte(":")
yamlModelKey := []byte(rootModelKey + ":")
yamlDefaultOrderKey := []byte(rootDefaultOrderKey + ":")
yamlIndexesKey := []byte(rootIndexesKey + ":")
yamlAttrKey := []byte(rootAttributesKey + ":")
yamlAttrRelation := []byte(rootRelationsKey + ":")
Expand All @@ -249,7 +255,7 @@ func (s *specification) Write(writer io.Writer) error {

for i, line := range lines {

if bytes.Equal(line, yamlIndexesKey) {
if bytes.Equal(line, yamlIndexesKey) || bytes.Equal(line, yamlDefaultOrderKey) {
inIndexes = true
} else if bytes.Equal(line, yamlAttrKey) {
inIndexes = false
Expand All @@ -272,6 +278,12 @@ func (s *specification) Write(writer io.Writer) error {
}
buf.WriteString("# Model\n")
}
if bytes.Equal(line, yamlDefaultOrderKey) {
if !condFirstLine {
buf.WriteRune('\n')
}
buf.WriteString("# Ordering\n")
}
if bytes.Equal(line, yamlIndexesKey) {
if !condFirstLine {
buf.WriteRune('\n')
Expand Down Expand Up @@ -361,6 +373,7 @@ func (s *specification) Validate() []error {
}

func (s *specification) Model() *Model {

return s.RawModel
}

Expand All @@ -376,9 +389,15 @@ func (s *specification) Indexes() [][]string {
}
return strings.Compare(s.RawIndexes[i][0], s.RawIndexes[j][0]) != -1
})

return s.RawIndexes
}

func (s *specification) DefaultOrder() []string {

return s.RawDefaultOrder
}

func (s *specification) Relations() []*Relation {
return s.RawRelations
}
Expand Down Expand Up @@ -478,19 +497,6 @@ func (s *specification) Identifier() *Attribute {
return s.identifier
}

// OrderingAttributes returns all the ordering attribute.
func (s *specification) OrderingAttributes(version string) []*Attribute {

var out []*Attribute // nolint
versions := s.versionsFrom(version)

for _, v := range versions {
out = append(out, s.orderingAttributes[v]...)
}

return out
}

// ApplyBaseSpecifications applies attributes of the given *Specifications to the receiver.
func (s *specification) ApplyBaseSpecifications(specs ...Specification) error {

Expand Down Expand Up @@ -596,7 +602,6 @@ func (s *specification) ValidationProviders() []string {
func (s *specification) buildAttributesMapping() error {

s.attributeMap = attributeMapping{}
s.orderingAttributes = versionedAttributes{}
s.identifier = nil

for version, attrs := range s.RawAttributes {
Expand All @@ -605,10 +610,6 @@ func (s *specification) buildAttributesMapping() error {
s.attributeMap[version] = map[string]*Attribute{}
}

if _, ok := s.orderingAttributes[version]; !ok {
s.orderingAttributes[version] = []*Attribute{}
}

for _, attr := range attrs {

attr.linkedSpecification = s
Expand All @@ -623,10 +624,6 @@ func (s *specification) buildAttributesMapping() error {

s.attributeMap[version][attr.Name] = attr

if attr.DefaultOrder {
s.orderingAttributes[version] = append(s.orderingAttributes[version], attr)
}

if attr.Identifier {
if s.identifier != nil {
return fmt.Errorf("Specification %s has more than one identifier attributes: At least %s and %s", s.RawModel.RestName, s.identifier.Name, attr.Name)
Expand Down
Loading

0 comments on commit 9561ebf

Please sign in to comment.