Skip to content

Commit

Permalink
eliminate attributed
Browse files Browse the repository at this point in the history
  • Loading branch information
reuvenharrison committed Sep 7, 2024
1 parent 8ba7ec9 commit 4147fde
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 58 deletions.
19 changes: 10 additions & 9 deletions checker/generator/checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,26 +52,23 @@ func concat(list []string) string {
return strings.Join(copy, "-")
}

func getHierarchyPostfix(action string, hierarchy []string, atttibuted []bool) string {
func getHierarchyPostfix(action string, hierarchy []string) string {
if len(hierarchy) == 0 {
return ""
}

return getPreposition(action) + " " + getHierarchyMessage(hierarchy, atttibuted)
return getPreposition(action) + " " + getHierarchyMessage(hierarchy)
}

func getHierarchyMessage(hierarchy []string, atttibuted []bool) string {
func getHierarchyMessage(hierarchy []string) string {

copy := slices.Clone(hierarchy)

if atttibuted != nil {
for i, s := range hierarchy {
if atttibuted[i] {
copy[i] = "%s " + s
}
for i, s := range hierarchy {
if isAtttibuted(s) {
copy[i] = "%s " + s
}
}

result := strings.Join(copy, " %s of ")

if hierarchy != nil && !isTopLevel(hierarchy[len(hierarchy)-1]) {
Expand All @@ -86,6 +83,10 @@ func isTopLevel(s string) bool {
s == "paths"
}

func isAtttibuted(s string) bool {
return s == "request parameter"
}

func standardizeSpaces(s string) string {
return strings.Join(strings.Fields(s), " ")
}
Expand Down
41 changes: 14 additions & 27 deletions checker/generator/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import "slices"

func getAll() ValueSets {
return slices.Concat(
getPaths(),
getEndpoints(),
getRequest(),
getResponse(),
Expand All @@ -14,26 +13,22 @@ func getAll() ValueSets {

func getRequest() ValueSets {
return slices.Concat(
NewValueSets([]string{"media-type", "request body"}, nil, schemaValueSets),
NewValueSets([]string{"property", "media-type", "request body"}, nil, schemaValueSets),
NewValueSets([]string{"request parameter"}, []bool{true}, schemaValueSets),
NewValueSets(nil, nil, operationValueSets),
NewValueSets([]string{"media-type", "request body"}, schemaValueSets),
NewValueSets([]string{"property", "media-type", "request body"}, schemaValueSets),
NewValueSets([]string{"request parameter"}, schemaValueSets),
NewValueSets(nil, operationValueSets),
)
}

func getResponse() ValueSets {
return slices.Concat(
NewValueSets([]string{"media-type", "response"}, nil, schemaValueSets),
NewValueSets([]string{"property", "media-type", "response"}, nil, schemaValueSets),
NewValueSets([]string{"media-type", "response"}, schemaValueSets),
NewValueSets([]string{"property", "media-type", "response"}, schemaValueSets),
)
}

func getPaths() ValueSets {
return NewValueSets(nil, nil, pathValueSets)
}

func getEndpoints() ValueSets {
return NewValueSets(nil, nil, endpointValueSets)
return NewValueSets(nil, endpointValueSets)
}

func getComponents() ValueSets {
Expand All @@ -43,7 +38,7 @@ func getComponents() ValueSets {
}

func getSecurity() ValueSets {
return NewValueSets(nil, nil, securityValueSets)
return NewValueSets(nil, securityValueSets)
}

var securityValueSets = ValueSets{
Expand All @@ -55,33 +50,28 @@ var securityValueSets = ValueSets{
ValueSetB{
predicativeAdjective: "%s",
hierarchy: []string{"global security scheme"},
attributed: []bool{false},
nouns: []string{"security scope"},
actions: []string{"add", "remove"},
},
}

var pathValueSets = ValueSets{
ValueSetB{
nouns: []string{"endpoint"},
actions: []string{"add", "remove", "deprecate", "reactivate"},
},
}

var endpointValueSets = ValueSets{
ValueSetA{
nouns: []string{"stability"},
nouns: []string{"stability"}, // /Paths/PathItem/Operation
actions: []string{"decrease"},
},
ValueSetB{
nouns: []string{"endpoint"}, // /Paths/PathItem
actions: []string{"add", "remove", "deprecate", "reactivate"},
},
ValueSetB{
predicativeAdjective: "%s",
nouns: []string{"success response status", "non-success response status"},
nouns: []string{"success response status", "non-success response status"}, // /Paths/PathItem/Operation/Responses/Response/content/media-type/
actions: []string{"add", "remove"},
},
ValueSetB{
predicativeAdjective: "%s",
hierarchy: []string{"endpoint security scheme"},
attributed: []bool{false},
nouns: []string{"security scope"},
actions: []string{"add", "remove"},
},
Expand Down Expand Up @@ -134,21 +124,18 @@ var schemaValueSets = ValueSets{
ValueSetB{
predicativeAdjective: "%s",
hierarchy: []string{"anyOf list"},
attributed: []bool{false},
nouns: []string{"schema"},
actions: []string{"add", "remove"},
},
ValueSetB{
predicativeAdjective: "%s",
hierarchy: []string{"anyOf list"},
attributed: []bool{false},
nouns: []string{"schema"},
actions: []string{"add", "remove"},
},
ValueSetB{
predicativeAdjective: "%s",
hierarchy: []string{"anyOf list"},
attributed: []bool{false},
nouns: []string{"schema"},
actions: []string{"add", "remove"},
},
Expand Down
2 changes: 1 addition & 1 deletion checker/generator/messages.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
stability-decreased: stability was decreased from %s to %s
endpoint-added: added endpoint
endpoint-removed: removed endpoint
endpoint-deprecated: deprecated endpoint
endpoint-reactivated: reactivated endpoint
stability-decreased: stability was decreased from %s to %s
success-response-status-added: added success response status %s
success-response-status-removed: removed success response status %s
non-success-response-status-added: added non-success response status %s
Expand Down
35 changes: 14 additions & 21 deletions checker/generator/value_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,12 @@ import (
type ValueSets []IValueSet

// NewValueSets creates a new ValueSets object
// attributed is a list of booleans that indicates if the level in the hierarchy should be preceded by a %s
func NewValueSets(hierarchy []string, attributed []bool, valueSets ValueSets) ValueSets {
func NewValueSets(hierarchy []string, valueSets ValueSets) ValueSets {

result := make(ValueSets, len(valueSets))

if attributed == nil {
attributed = make([]bool, len(hierarchy))
}

for i, vs := range valueSets {
result[i] = vs.setHierarchy(hierarchy, attributed)
result[i] = vs.setHierarchy(hierarchy)
}

return result
Expand All @@ -33,40 +28,38 @@ func (vs ValueSets) generate(out io.Writer) {

type IValueSet interface {
generate(out io.Writer)
setHierarchy(hierarchy []string, attributed []bool) IValueSet
setHierarchy(hierarchy []string) IValueSet
}

type ValueSet struct {
attributiveAdjective string // attributive adjectives are added before the noun
predicativeAdjective string // predicative adjectives are added after the noun
hierarchy []string
attributed []bool // attributed levels in the hierarchy are preceded by a name (%s)
nouns []string
actions []string
}

func (v ValueSet) setHierarchy(hierarchy []string, attributed []bool) ValueSet {
func (v ValueSet) setHierarchy(hierarchy []string) ValueSet {
if len(hierarchy) == 0 {
return v
}

v.hierarchy = append(v.hierarchy, hierarchy...)
v.attributed = append(v.attributed, attributed...)

return v
}

// ValueSetA messages start with the noun
type ValueSetA ValueSet

func (v ValueSetA) setHierarchy(hierarchy []string, attributed []bool) IValueSet {
return ValueSetA(ValueSet(v).setHierarchy(hierarchy, attributed))
func (v ValueSetA) setHierarchy(hierarchy []string) IValueSet {
return ValueSetA(ValueSet(v).setHierarchy(hierarchy))
}

func (v ValueSetA) generate(out io.Writer) {
generateMessage := func(hierarchy []string, atttibuted []bool, noun, attributiveAdjective, predicativeAdjective, action string) string {
generateMessage := func(hierarchy []string, noun, attributiveAdjective, predicativeAdjective, action string) string {
prefix := addAttribute(noun, attributiveAdjective, predicativeAdjective)
if hierarchyMessage := getHierarchyMessage(hierarchy, atttibuted); hierarchyMessage != "" {
if hierarchyMessage := getHierarchyMessage(hierarchy); hierarchyMessage != "" {
prefix += " of " + hierarchyMessage
}

Expand All @@ -76,7 +69,7 @@ func (v ValueSetA) generate(out io.Writer) {
for _, noun := range v.nouns {
for _, action := range v.actions {
id := generateId(v.hierarchy, noun, action)
message := generateMessage(v.hierarchy, v.attributed, noun, v.attributiveAdjective, v.predicativeAdjective, action)
message := generateMessage(v.hierarchy, noun, v.attributiveAdjective, v.predicativeAdjective, action)
fmt.Fprintln(out, fmt.Sprintf("%s: %s", id, message))

Check failure on line 73 in checker/generator/value_set.go

View workflow job for this annotation

GitHub Actions / lint

S1038: should use fmt.Fprintf instead of fmt.Fprintln(fmt.Sprintf(...)) (but don't forget the newline) (gosimple)
}
}
Expand All @@ -85,18 +78,18 @@ func (v ValueSetA) generate(out io.Writer) {
// ValueSetB messages start with the action
type ValueSetB ValueSet

func (v ValueSetB) setHierarchy(hierarchy []string, attributed []bool) IValueSet {
return ValueSetB(ValueSet(v).setHierarchy(hierarchy, attributed))
func (v ValueSetB) setHierarchy(hierarchy []string) IValueSet {
return ValueSetB(ValueSet(v).setHierarchy(hierarchy))
}

func (v ValueSetB) generate(out io.Writer) {
generateMessage := func(hierarchy []string, atttibuted []bool, noun, attributiveAdjective, predicativeAdjective, action string) string {
return standardizeSpaces(strings.Join([]string{conjugate(action), addAttribute(noun, attributiveAdjective, predicativeAdjective), getHierarchyPostfix(action, hierarchy, atttibuted)}, " "))
generateMessage := func(hierarchy []string, noun, attributiveAdjective, predicativeAdjective, action string) string {
return standardizeSpaces(strings.Join([]string{conjugate(action), addAttribute(noun, attributiveAdjective, predicativeAdjective), getHierarchyPostfix(action, hierarchy)}, " "))
}

for _, noun := range v.nouns {
for _, action := range v.actions {
fmt.Fprintln(out, fmt.Sprintf("%s: %s", generateId(v.hierarchy, noun, action), generateMessage(v.hierarchy, v.attributed, noun, v.attributiveAdjective, v.predicativeAdjective, action)))
fmt.Fprintln(out, fmt.Sprintf("%s: %s", generateId(v.hierarchy, noun, action), generateMessage(v.hierarchy, noun, v.attributiveAdjective, v.predicativeAdjective, action)))

Check failure on line 92 in checker/generator/value_set.go

View workflow job for this annotation

GitHub Actions / lint

S1038: should use fmt.Fprintf instead of fmt.Fprintln(fmt.Sprintf(...)) (but don't forget the newline) (gosimple)
}
}
}

0 comments on commit 4147fde

Please sign in to comment.