Skip to content

Commit

Permalink
generate messages from yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
reuvenharrison committed Sep 9, 2024
1 parent cd1ba93 commit b42c0b2
Show file tree
Hide file tree
Showing 6 changed files with 247 additions and 84 deletions.
6 changes: 5 additions & 1 deletion checker/generator/checks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,9 @@ import (
)

func TestGenerator(t *testing.T) {
require.NoError(t, generator.Generate())
require.NoError(t, generator.Generate(generator.GetAll))
}

func TestTreeGenerator(t *testing.T) {
require.NoError(t, generator.Generate(generator.GetTree))
}
130 changes: 65 additions & 65 deletions checker/generator/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package generator

import "slices"

func getAll() ValueSets {
func GetAll() (MessageGenerator, error) {
return slices.Concat(
getEndpoints(),
getRequest(),
getResponse(),
getComponents(),
)
), nil
}

func getRequest() ValueSets {
Expand Down Expand Up @@ -43,120 +43,120 @@ func getSecurity() ValueSets {

var securityValueSets = ValueSets{
ValueSetB{
predicativeAdjective: "%s",
objects: []string{"endpoint scheme security"},
actions: []string{"add", "remove"},
PredicativeAdjective: "%s",
Names: []string{"endpoint scheme security"},
Actions: []string{"add", "remove"},
},
ValueSetB{
predicativeAdjective: "%s",
hierarchy: []string{"global security scheme"},
objects: []string{"security scope"},
actions: []string{"add", "remove"},
PredicativeAdjective: "%s",
Hierarchy: []string{"global security scheme"},
Names: []string{"security scope"},
Actions: []string{"add", "remove"},
},
}

var endpointValueSets = ValueSets{
ValueSetA{
objects: []string{"stability"}, // /Paths/PathItem/Operation
actions: []string{"decrease"},
Names: []string{"stability"}, // /Paths/PathItem/Operation
Actions: []string{"decrease"},
},
ValueSetA{
objects: []string{"api path", "api"},
actions: []string{"remove"},
adverb: []string{"without deprecation", "before sunset"},
Names: []string{"api path", "api"},
Actions: []string{"remove"},
Adverbs: []string{"without deprecation", "before sunset"},
},
ValueSetB{
objects: []string{"endpoint"}, // /Paths/PathItem
actions: []string{"add", "remove", "deprecate", "reactivate"},
Names: []string{"endpoint"}, // /Paths/PathItem
Actions: []string{"add", "remove", "deprecate", "reactivate"},
},
ValueSetB{
predicativeAdjective: "%s",
objects: []string{"success response status", "non-success response status"}, // /Paths/PathItem/Operation/Responses/Response/content/media-type/
actions: []string{"add", "remove"},
PredicativeAdjective: "%s",
Names: []string{"success response status", "non-success response status"}, // /Paths/PathItem/Operation/Responses/Response/content/media-type/
Actions: []string{"add", "remove"},
},
ValueSetA{
objects: []string{"operation id"},
actions: []string{"change"},
Names: []string{"operation id"},
Actions: []string{"change"},
},
ValueSetB{
predicativeAdjective: "%s",
objects: []string{"operation id", "tag"},
actions: []string{"add", "remove"},
PredicativeAdjective: "%s",
Names: []string{"operation id", "tag"},
Actions: []string{"add", "remove"},
},
ValueSetB{
predicativeAdjective: "%s",
hierarchy: []string{"endpoint security scheme"},
objects: []string{"security scope"},
actions: []string{"add", "remove"},
PredicativeAdjective: "%s",
Hierarchy: []string{"endpoint security scheme"},
Names: []string{"security scope"},
Actions: []string{"add", "remove"},
},
}

var operationValueSets = ValueSets{
ValueSetB{
objects: []string{"required request body", "optional request body"},
actions: []string{"add", "remove"},
Names: []string{"required request body", "optional request body"},
Actions: []string{"add", "remove"},
},
ValueSetB{
predicativeAdjective: "%s",
attributiveAdjective: "%s",
objects: []string{"request parameter"},
actions: []string{"add", "remove"},
PredicativeAdjective: "%s",
AttributiveAdjective: "%s",
Names: []string{"request parameter"},
Actions: []string{"add", "remove"},
},
}

var schemaValueSets = ValueSets{
ValueSetA{
predicativeAdjective: "value",
objects: []string{"max", "maxLength", "min", "minLength", "minItems", "maxItems"},
actions: []string{"set", "increase", "decrease"},
PredicativeAdjective: "value",
Names: []string{"max", "maxLength", "min", "minLength", "minItems", "maxItems"},
Actions: []string{"set", "increase", "decrease"},
},
ValueSetA{
objects: []string{"type/format"},
actions: []string{"change", "generalize"},
Names: []string{"type/format"},
Actions: []string{"change", "generalize"},
},
ValueSetA{
objects: []string{"discriminator property name"},
actions: []string{"change"},
Names: []string{"discriminator property name"},
Actions: []string{"change"},
},
ValueSetA{
objects: []string{"pattern"},
actions: []string{"change", "generalize"},
Names: []string{"pattern"},
Actions: []string{"change", "generalize"},
},
ValueSetA{
objects: []string{"required property", "optional property"},
actions: []string{"change"},
Names: []string{"required property", "optional property"},
Actions: []string{"change"},
},
ValueSetB{
predicativeAdjective: "%s",
objects: []string{"pattern"},
actions: []string{"add", "remove"},
PredicativeAdjective: "%s",
Names: []string{"pattern"},
Actions: []string{"add", "remove"},
},
ValueSetB{
objects: []string{"default value"},
actions: []string{"add", "remove"},
Names: []string{"default value"},
Actions: []string{"add", "remove"},
},
ValueSetB{
predicativeAdjective: "%s",
hierarchy: []string{"anyOf list"},
objects: []string{"schema"},
actions: []string{"add", "remove"},
PredicativeAdjective: "%s",
Hierarchy: []string{"anyOf list"},
Names: []string{"schema"},
Actions: []string{"add", "remove"},
},
ValueSetB{
predicativeAdjective: "%s",
hierarchy: []string{"anyOf list"},
objects: []string{"schema"},
actions: []string{"add", "remove"},
PredicativeAdjective: "%s",
Hierarchy: []string{"anyOf list"},
Names: []string{"schema"},
Actions: []string{"add", "remove"},
},
ValueSetB{
predicativeAdjective: "%s",
hierarchy: []string{"anyOf list"},
objects: []string{"schema"},
actions: []string{"add", "remove"},
PredicativeAdjective: "%s",
Hierarchy: []string{"anyOf list"},
Names: []string{"schema"},
Actions: []string{"add", "remove"},
},
ValueSetB{
predicativeAdjective: "%s",
objects: []string{"discriminator", "mapping keys"},
actions: []string{"add", "remove"},
PredicativeAdjective: "%s",
Names: []string{"discriminator", "mapping keys"},
Actions: []string{"add", "remove"},
},
}

Expand Down
15 changes: 13 additions & 2 deletions checker/generator/generator.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,33 @@
package generator

import (
"io"
"os"
"slices"
"strings"

"github.com/iancoleman/strcase"
)

func Generate() error {
type MessageGenerator interface {
generate(out io.Writer)
}

type Getter func() (MessageGenerator, error)

func Generate(getter Getter) error {
out, err := os.Create("messages.yaml")
if err != nil {
return err
}
defer out.Close()

getAll().generate(out)
data, err := getter()
if err != nil {
return err
}

data.generate(out)
return nil
}

Expand Down
86 changes: 86 additions & 0 deletions checker/generator/tree.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package generator

import (
"fmt"
"io"
"os"

"gopkg.in/yaml.v3"
)

type ChangeMap map[string]Changes

type Changes struct {
ExcludeFromHierarchy bool `yaml:"excludeFromHierarchy"`
Actions map[string]Objects `yaml:"actions"`
NextLevel ChangeMap `yaml:"nextLevel"`
}

type Objects []*Object

type Object struct {
Hierarchy []string `yaml:"hierarchy"`
Object string `yaml:"object"`
Adverbs []string `yaml:"adverbs"`
StartWith string `yaml:"startWith"`
PredicativeAdjective string `yaml:"predicativeAdjective"`
AttributiveAdjective string `yaml:"attributiveAdjective"`
}

func GetTree() (MessageGenerator, error) {
yamlFile, err := os.ReadFile("tree.yaml")
if err != nil {
return nil, fmt.Errorf("yamlFile.Get err #%v ", err)
}

var changeMap ChangeMap
err = yaml.Unmarshal(yamlFile, &changeMap)
if err != nil {
return nil, fmt.Errorf("unmarshal: %v", err)
}

return changeMap, nil
}

func (changeMap ChangeMap) generate(out io.Writer) {
fillHierarchy(changeMap, nil)

for _, change := range changeMap {
for action, objects := range change.Actions {
for _, object := range objects {
getValueSet(object, action).generate(out)
}
}
change.NextLevel.generate(out)
}
}

func fillHierarchy(changeMap ChangeMap, hierarchy []string) {
for container, change := range changeMap {
if !change.ExcludeFromHierarchy {
hierarchy = append([]string{container}, hierarchy...)
}
for _, objects := range change.Actions {
for _, object := range objects {
object.Hierarchy = hierarchy
}
}
fillHierarchy(change.NextLevel, hierarchy)
}
}

func getValueSet(object *Object, action string) IValueSet {
valueSet := ValueSet{
AttributiveAdjective: object.AttributiveAdjective,
PredicativeAdjective: object.PredicativeAdjective,
Hierarchy: object.Hierarchy,
Names: []string{object.Object},
Actions: []string{action},
Adverbs: object.Adverbs,
}

if object.StartWith == "object" {
return ValueSetA(valueSet)
}
return ValueSetB(valueSet)
}
60 changes: 60 additions & 0 deletions checker/generator/tree.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
paths:
excludeFromHierarchy: true
actions:
remove:
- object: path
adverbs:
- without deprecation
- before sunset
startWith: object
nextLevel:
operations:
excludeFromHierarchy: true
actions:
remove:
- object: endpoint
adverbs:
- without deprecation
- before sunset
startWith: object
- object: endpoint
startWith: action
add:
- object: endpoint
startWith: action
deprecate:
- object: endpoint
startWith: action
reactivate:
- object: endpoint
startWith: action
nextLevel:
endpoint:
excludeFromHierarchy: true
actions:
change:
- object: operation id
startWith: object
decrease:
- object: stability
startWith: object
nextLevel:
responses:
excludeFromHierarchy: true
actions:
add:
- object: success response
predicativeAdjective: "status %s"
startWith: action
- object: non-success response
predicativeAdjective: "status %s"
startWith: action
remove:
- object: success response
predicativeAdjective: "status %s"
startWith: action
- object: non-success response
predicativeAdjective: "status %s"
startWith: action


Loading

0 comments on commit b42c0b2

Please sign in to comment.