Skip to content

Commit

Permalink
Clean up parsing code a bit.
Browse files Browse the repository at this point in the history
  • Loading branch information
bilus committed Oct 23, 2021
1 parent dcb416f commit 38d79d1
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 53 deletions.
3 changes: 0 additions & 3 deletions features/repl.feature
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ Feature: REPL
Background:
Given I'm in project dir

@current
Scenario: Successfully run a command
Given file ./Oyafile containing
"""
Expand All @@ -14,7 +13,6 @@ Scenario: Successfully run a command
And I send "exit" to repl
Then file ./OK exists

@current
Scenario: Access a value
Given file ./Oyafile containing
"""
Expand All @@ -31,7 +29,6 @@ Scenario: Access a value
foo: bar
"""

@current
Scenario: Pass an environmental variable
Given file ./Oyafile containing
"""
Expand Down
97 changes: 47 additions & 50 deletions pkg/oyafile/parsing.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ import (
"github.com/tooploox/oya/pkg/types"
)

const StmtImport = "Import"
const StmtValues = "Values"
const StmtProject = "Project"
const StmtIgnore = "Ignore"
const StmtChangeset = "Changeset"
const StmtRequire = "Require"
const StmtReplace = "Replace"

const ChangesetTaskName = StmtChangeset

func Parse(raw *raw.Oyafile) (*Oyafile, error) {
of, err := raw.Decode()
if err != nil {
Expand All @@ -26,54 +36,9 @@ func Parse(raw *raw.Oyafile) (*Oyafile, error) {
if !ok {
return nil, errors.Errorf("Incorrect value name: %v", name)
}
switch name {
case "Import":
err := parseImports(value, oyafile)
if err != nil {
return nil, errors.Wrapf(err, "error parsing key %q", name)
}
case "Values":
err := parseValues(value, oyafile)
if err != nil {
return nil, errors.Wrapf(err, "error parsing key %q", name)
}
case "Project":
err := parseProject(value, oyafile)
if err != nil {
return nil, errors.Wrapf(err, "error parsing key %q", name)
}
case "Ignore":
err := parseIgnore(value, oyafile)
if err != nil {
return nil, errors.Wrapf(err, "error parsing key %q", name)
}
case "Changeset":
err := parseTask(name, value, oyafile)
if err != nil {
return nil, errors.Wrapf(err, "error parsing key %q", name)
}
case "Require":
err := parseRequire(name, value, oyafile)
if err != nil {
return nil, errors.Wrapf(err, "error parsing key %q", name)
}
case "Replace":
err := parseReplace(name, value, oyafile)
if err != nil {
return nil, errors.Wrapf(err, "error parsing key %q", name)
}

default:
taskName := task.Name(name)
if taskName.IsBuiltIn() {
log.Debugf("WARNING: Unrecognized built-in task or directive %q; skipping.", name)
continue
}

err := parseTask(name, value, oyafile)
if err != nil {
return nil, errors.Wrapf(err, "error parsing key %q", name)
}
err := parseStatement(name, value, oyafile)
if err != nil {
return nil, errors.Wrapf(err, "error parsing key %q", name)
}
}

Expand All @@ -99,6 +64,34 @@ func (oyafile *Oyafile) resolveReplacements() error {
return nil
}

func parseStatement(name string, value interface{}, o *Oyafile) error {
switch name {
case StmtImport:
return parseImports(value, o)
case StmtValues:
return parseValues(value, o)
case StmtProject:
return parseProject(value, o)
case StmtIgnore:
return parseIgnore(value, o)
case StmtChangeset:
return parseChangeset(value, o)
case StmtRequire:
return parseRequire(value, o)
case StmtReplace:
return parseReplace(value, o)

default:
taskName := task.Name(name)
if taskName.IsBuiltIn() {
log.Debugf("WARNING: Unrecognized built-in task or directive %q; skipping.", name)
return nil
}

return parseTask(name, value, o)
}
}

func parseMeta(metaName, key string) (task.Name, bool) {
taskName := strings.TrimSuffix(key, "."+metaName)
return task.Name(taskName), taskName != key
Expand Down Expand Up @@ -170,6 +163,10 @@ func parseIgnore(value interface{}, o *Oyafile) error {
return nil
}

func parseChangeset(value interface{}, o *Oyafile) error {
return parseTask(ChangesetTaskName, value, o)
}

func parseTask(name string, value interface{}, o *Oyafile) error {
s, ok := value.(string)
if !ok {
Expand All @@ -186,7 +183,7 @@ func parseTask(name string, value interface{}, o *Oyafile) error {
return nil
}

func parseRequire(name string, value interface{}, o *Oyafile) error {
func parseRequire(value interface{}, o *Oyafile) error {
if value == nil {
return nil
}
Expand Down Expand Up @@ -223,7 +220,7 @@ func parseRequire(name string, value interface{}, o *Oyafile) error {
return nil
}

func parseReplace(name string, value interface{}, o *Oyafile) error {
func parseReplace(value interface{}, o *Oyafile) error {
defaultErr := fmt.Errorf("expected entries mapping pack import paths to paths relative to the project root directory, example: \"github.com/tooploox/oya-pack/docker: /packs/docker\"")

replacements, ok := value.(map[interface{}]interface{})
Expand Down

0 comments on commit 38d79d1

Please sign in to comment.