Skip to content

Commit

Permalink
API change for read tag overrides
Browse files Browse the repository at this point in the history
  • Loading branch information
tliron committed Mar 30, 2020
1 parent 76491cc commit 7a509ed
Show file tree
Hide file tree
Showing 20 changed files with 71 additions and 118 deletions.
71 changes: 39 additions & 32 deletions tosca/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ type Context struct {
Problems *problems.Problems
Quirks Quirks
Grammar *Grammar
ReadOverrides map[string]string
ReadTagOverrides map[string]string
}

func NewContext(quirks Quirks) *Context {
Expand Down Expand Up @@ -89,24 +89,6 @@ func (self *Context) NewImportContext(url urlpkg.URL) *Context {
}
}

func (self *Context) Clone(data interface{}) *Context {
return &Context{
Parent: self.Parent,
Name: self.Name,
Path: self.Path,
URL: self.URL,
Data: data,
Locator: self.Locator,
CanonicalNamespace: self.CanonicalNamespace,
Namespace: self.Namespace,
ScriptletNamespace: self.ScriptletNamespace,
Hierarchy: self.Hierarchy,
Problems: self.Problems,
Quirks: self.Quirks,
Grammar: self.Grammar,
}
}

func (self *Context) GetAncestor(generation int) *Context {
if generation == 0 {
return self
Expand All @@ -128,23 +110,17 @@ func (self *Context) GetCanonicalNamespace() *string {
return nil
}

func (self *Context) Is(typeNames ...string) bool {
for _, typeName := range typeNames {
if typeValidator, ok := ard.TypeValidators[typeName]; ok {
if typeValidator(self.Data) {
return true
}
} else {
panic(fmt.Sprintf("unsupported field type: %s", typeName))
}
}
return false
}

func (self *Context) HasQuirk(quirk Quirk) bool {
return self.Quirks.Has(quirk)
}

func (self *Context) SetReadTag(fieldName string, tag string) {
if self.ReadTagOverrides == nil {
self.ReadTagOverrides = make(map[string]string)
}
self.ReadTagOverrides[fieldName] = tag
}

func (self *Context) GetLocation() (int, int) {
if self.Locator != nil {
if row, column, ok := self.Locator.Locate(self.Path...); ok {
Expand All @@ -154,10 +130,41 @@ func (self *Context) GetLocation() (int, int) {
return -1, -1
}

func (self *Context) Is(typeNames ...string) bool {
for _, typeName := range typeNames {
if typeValidator, ok := ard.TypeValidators[typeName]; ok {
if typeValidator(self.Data) {
return true
}
} else {
panic(fmt.Sprintf("unsupported field type: %s", typeName))
}
}
return false
}

//
// Child contexts
//

func (self *Context) Clone(data interface{}) *Context {
return &Context{
Parent: self.Parent,
Name: self.Name,
Path: self.Path,
URL: self.URL,
Data: data,
Locator: self.Locator,
CanonicalNamespace: self.CanonicalNamespace,
Namespace: self.Namespace,
ScriptletNamespace: self.ScriptletNamespace,
Hierarchy: self.Hierarchy,
Problems: self.Problems,
Quirks: self.Quirks,
Grammar: self.Grammar,
}
}

func (self *Context) FieldChild(name interface{}, data interface{}) *Context {
nameString := yamlkeys.KeyString(name) // complex keys would be stringified
return &Context{
Expand Down
5 changes: 1 addition & 4 deletions tosca/grammars/tosca_v1_0/policy-type.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ import (

// tosca.Reader signature
func ReadPolicyType(context *tosca.Context) interface{} {
if context.ReadOverrides == nil {
context.ReadOverrides = make(map[string]string)
}
context.ReadOverrides["TriggerDefinitions"] = ""
context.SetReadTag("TriggerDefinitions", "")

return tosca_v1_3.ReadPolicyType(context)
}
5 changes: 1 addition & 4 deletions tosca/grammars/tosca_v1_0/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ import (

// tosca.Reader signature
func ReadPolicy(context *tosca.Context) interface{} {
if context.ReadOverrides == nil {
context.ReadOverrides = make(map[string]string)
}
context.ReadOverrides["TriggerDefinitions"] = ""
context.SetReadTag("TriggerDefinitions", "")

return tosca_v1_3.ReadPolicy(context)
}
5 changes: 1 addition & 4 deletions tosca/grammars/tosca_v1_0/topology-template.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ import (

// tosca.Reader signature
func ReadTopologyTemplate(context *tosca.Context) interface{} {
if context.ReadOverrides == nil {
context.ReadOverrides = make(map[string]string)
}
context.ReadOverrides["WorkflowDefinitions"] = ""
context.SetReadTag("WorkflowDefinitions", "")

return tosca_v1_3.ReadTopologyTemplate(context)
}
7 changes: 2 additions & 5 deletions tosca/grammars/tosca_v1_1/interface-implementation.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,8 @@ import (

// tosca.Reader signature
func ReadInterfaceImplementation(context *tosca.Context) interface{} {
if context.ReadOverrides == nil {
context.ReadOverrides = make(map[string]string)
}
context.ReadOverrides["Timeout"] = ""
context.ReadOverrides["OperationHost"] = ""
context.SetReadTag("Timeout", "")
context.SetReadTag("OperationHost", "")

return tosca_v1_3.ReadInterfaceImplementation(context)
}
7 changes: 2 additions & 5 deletions tosca/grammars/tosca_v1_1/substitution-mappings.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,8 @@ import (

// tosca.Reader signature
func ReadSubstitutionMappings(context *tosca.Context) interface{} {
if context.ReadOverrides == nil {
context.ReadOverrides = make(map[string]string)
}
context.ReadOverrides["PropertyMappings"] = ""
context.ReadOverrides["InterfaceMappings"] = ""
context.SetReadTag("PropertyMappings", "")
context.SetReadTag("InterfaceMappings", "")

return tosca_v1_2.ReadSubstitutionMappings(context)
}
5 changes: 1 addition & 4 deletions tosca/grammars/tosca_v1_1/unit.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ import (

// tosca.Reader signature
func ReadUnit(context *tosca.Context) interface{} {
if context.ReadOverrides == nil {
context.ReadOverrides = make(map[string]string)
}
context.ReadOverrides["Namespace"] = ""
context.SetReadTag("Namespace", "")

self := tosca_v1_3.NewUnit(context)
context.ScriptletNamespace.Merge(DefaultScriptletNamespace)
Expand Down
9 changes: 3 additions & 6 deletions tosca/grammars/tosca_v1_2/artifact-definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,9 @@ import (

// tosca.Reader signature
func ReadArtifactDefinition(context *tosca.Context) interface{} {
if context.ReadOverrides == nil {
context.ReadOverrides = make(map[string]string)
}
context.ReadOverrides["ArtifactVersion"] = ""
context.ReadOverrides["ChecksumAlgorithm"] = ""
context.ReadOverrides["Checksum"] = ""
context.SetReadTag("ArtifactVersion", "")
context.SetReadTag("ChecksumAlgorithm", "")
context.SetReadTag("Checksum", "")

return tosca_v1_3.ReadArtifactDefinition(context)
}
9 changes: 3 additions & 6 deletions tosca/grammars/tosca_v1_2/artifact.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,9 @@ import (

// tosca.Reader signature
func ReadArtifact(context *tosca.Context) interface{} {
if context.ReadOverrides == nil {
context.ReadOverrides = make(map[string]string)
}
context.ReadOverrides["ArtifactVersion"] = ""
context.ReadOverrides["ChecksumAlgorithm"] = ""
context.ReadOverrides["Checksum"] = ""
context.SetReadTag("ArtifactVersion", "")
context.SetReadTag("ChecksumAlgorithm", "")
context.SetReadTag("Checksum", "")

return tosca_v1_3.ReadArtifact(context)
}
7 changes: 2 additions & 5 deletions tosca/grammars/tosca_v1_2/attribute-definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@ import (

// tosca.Reader signature
func ReadAttributeDefinition(context *tosca.Context) interface{} {
if context.ReadOverrides == nil {
context.ReadOverrides = make(map[string]string)
}
context.ReadOverrides["Metadata"] = ""
context.ReadOverrides["KeySchema"] = ""
context.SetReadTag("Metadata", "")
context.SetReadTag("KeySchema", "")

return tosca_v1_3.ReadAttributeDefinition(context)
}
5 changes: 1 addition & 4 deletions tosca/grammars/tosca_v1_2/capability-assignment.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ import (

// tosca.Reader signature
func ReadCapabilityAssignment(context *tosca.Context) interface{} {
if context.ReadOverrides == nil {
context.ReadOverrides = make(map[string]string)
}
context.ReadOverrides["Occurrences"] = ""
context.SetReadTag("Occurrences", "")

return tosca_v1_3.ReadCapabilityAssignment(context)
}
5 changes: 1 addition & 4 deletions tosca/grammars/tosca_v1_2/group-type.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ import (

// tosca.Reader signature
func ReadGroupType(context *tosca.Context) interface{} {
if context.ReadOverrides == nil {
context.ReadOverrides = make(map[string]string)
}
context.ReadOverrides["InterfaceDefinitions"] = "interfaces,InterfaceDefinition"
context.SetReadTag("InterfaceDefinitions", "interfaces,InterfaceDefinition")

return tosca_v1_3.ReadGroupType(context)
}
5 changes: 1 addition & 4 deletions tosca/grammars/tosca_v1_2/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ import (

// tosca.Reader signature
func ReadGroup(context *tosca.Context) interface{} {
if context.ReadOverrides == nil {
context.ReadOverrides = make(map[string]string)
}
context.ReadOverrides["Interfaces"] = "interfaces,InterfaceAssignment"
context.SetReadTag("Interfaces", "interfaces,InterfaceAssignment")

return tosca_v1_3.ReadGroup(context)
}
7 changes: 2 additions & 5 deletions tosca/grammars/tosca_v1_2/interface-assignment.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@ import (

// tosca.Reader signature
func ReadInterfaceAssignment(context *tosca.Context) interface{} {
if context.ReadOverrides == nil {
context.ReadOverrides = make(map[string]string)
}
context.ReadOverrides["Operations"] = "?,OperationAssignment"
context.ReadOverrides["Notifications"] = ""
context.SetReadTag("Operations", "?,OperationAssignment")
context.SetReadTag("Notifications", "")

self := tosca_v1_3.NewInterfaceAssignment(context)
context.ReadFields(self)
Expand Down
7 changes: 2 additions & 5 deletions tosca/grammars/tosca_v1_2/interface-definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@ import (

// tosca.Reader signature
func ReadInterfaceDefinition(context *tosca.Context) interface{} {
if context.ReadOverrides == nil {
context.ReadOverrides = make(map[string]string)
}
context.ReadOverrides["OperationDefinitions"] = "?,OperationDefinition"
context.ReadOverrides["NotificationDefinitions"] = ""
context.SetReadTag("OperationDefinitions", "?,OperationDefinition")
context.SetReadTag("NotificationDefinitions", "")

self := tosca_v1_3.NewInterfaceDefinition(context)
context.ReadFields(self)
Expand Down
7 changes: 2 additions & 5 deletions tosca/grammars/tosca_v1_2/interface-type.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@ import (

// tosca.Reader signature
func ReadInterfaceType(context *tosca.Context) interface{} {
if context.ReadOverrides == nil {
context.ReadOverrides = make(map[string]string)
}
context.ReadOverrides["OperationDefinitions"] = "?,OperationDefinition"
context.ReadOverrides["NotificationDefinitions"] = ""
context.SetReadTag("OperationDefinitions", "?,OperationDefinition")
context.SetReadTag("NotificationDefinitions", "")

self := tosca_v1_3.NewInterfaceType(context)
context.ReadFields(self)
Expand Down
7 changes: 2 additions & 5 deletions tosca/grammars/tosca_v1_2/property-definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@ import (

// tosca.Reader signature
func ReadPropertyDefinition(context *tosca.Context) interface{} {
if context.ReadOverrides == nil {
context.ReadOverrides = make(map[string]string)
}
context.ReadOverrides["Metadata"] = ""
context.ReadOverrides["KeySchema"] = ""
context.SetReadTag("Metadata", "")
context.SetReadTag("KeySchema", "")

return tosca_v1_3.ReadPropertyDefinition(context)
}
7 changes: 2 additions & 5 deletions tosca/grammars/tosca_v1_2/substitution-mappings.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@ import (

// tosca.Reader signature
func ReadSubstitutionMappings(context *tosca.Context) interface{} {
if context.ReadOverrides == nil {
context.ReadOverrides = make(map[string]string)
}
context.ReadOverrides["AttributeMappings"] = ""
context.ReadOverrides["SubstitutionFilter"] = ""
context.SetReadTag("AttributeMappings", "")
context.SetReadTag("SubstitutionFilter", "")

return tosca_v1_3.ReadSubstitutionMappings(context)
}
5 changes: 1 addition & 4 deletions tosca/grammars/tosca_v1_3/substitution-mappings.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,7 @@ func NewSubstitutionMappings(context *tosca.Context) *SubstitutionMappings {
// tosca.Reader signature
func ReadSubstitutionMappings(context *tosca.Context) interface{} {
if context.HasQuirk(tosca.QuirkSubstitutionMappingsRequirementsList) {
if context.ReadOverrides == nil {
context.ReadOverrides = make(map[string]string)
}
context.ReadOverrides["RequirementMappings"] = "requirements,{}RequirementMapping"
context.SetReadTag("RequirementMappings", "requirements,{}RequirementMapping")
}

self := NewSubstitutionMappings(context)
Expand Down
4 changes: 2 additions & 2 deletions tosca/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ func (self *Context) ReadFields(entityPtr interface{}) []string {
tags := reflection.GetFieldTagsForValue(entity, "read")

// Read tag overrides
if self.ReadOverrides != nil {
for fieldName, tag := range self.ReadOverrides {
if self.ReadTagOverrides != nil {
for fieldName, tag := range self.ReadTagOverrides {
if tag != "" {
tags[fieldName] = tag
} else {
Expand Down

0 comments on commit 7a509ed

Please sign in to comment.