-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #43 from jaypipes/runnable
introduce `api.Runnable` back into the mix
- Loading branch information
Showing
13 changed files
with
636 additions
and
522 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Use and distribution licensed under the Apache license version 2. | ||
// | ||
// See the COPYING file in the root project directory for full text. | ||
|
||
package api | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
) | ||
|
||
// Runnable are things that Run a `*testing.T` | ||
type Runnable interface { | ||
// Run accepts a context and a `*testing.T` and runs some tests within that | ||
// context | ||
// | ||
// Errors returned by Run() are **RuntimeErrors**, not failures in | ||
// assertions. | ||
Run(context.Context, *testing.T) error | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
// Use and distribution licensed under the Apache license version 2. | ||
// | ||
// See the COPYING file in the root project directory for full text. | ||
|
||
package bar | ||
|
||
import ( | ||
"context" | ||
"strconv" | ||
|
||
"github.com/gdt-dev/gdt/api" | ||
"github.com/gdt-dev/gdt/plugin" | ||
"github.com/samber/lo" | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
func init() { | ||
plugin.Register(&Plugin{}) | ||
} | ||
|
||
type Defaults struct { | ||
Foo string `yaml:"bar"` | ||
} | ||
|
||
func (d *Defaults) UnmarshalYAML(node *yaml.Node) error { | ||
return nil | ||
} | ||
|
||
type Spec struct { | ||
api.Spec | ||
Bar int `yaml:"bar"` | ||
} | ||
|
||
func (s *Spec) SetBase(b api.Spec) { | ||
s.Spec = b | ||
} | ||
|
||
func (s *Spec) Base() *api.Spec { | ||
return &s.Spec | ||
} | ||
|
||
func (s *Spec) Retry() *api.Retry { | ||
return api.NoRetry | ||
} | ||
|
||
func (s *Spec) Timeout() *api.Timeout { | ||
return nil | ||
} | ||
|
||
func (s *Spec) Eval(context.Context) (*api.Result, error) { | ||
return api.NewResult(), nil | ||
} | ||
|
||
func (s *Spec) UnmarshalYAML(node *yaml.Node) error { | ||
if node.Kind != yaml.MappingNode { | ||
return api.ExpectedMapAt(node) | ||
} | ||
// maps/structs are stored in a top-level Node.Content field which is a | ||
// concatenated slice of Node pointers in pairs of key/values. | ||
for i := 0; i < len(node.Content); i += 2 { | ||
keyNode := node.Content[i] | ||
if keyNode.Kind != yaml.ScalarNode { | ||
return api.ExpectedScalarAt(keyNode) | ||
} | ||
key := keyNode.Value | ||
valNode := node.Content[i+1] | ||
switch key { | ||
case "bar": | ||
if valNode.Kind != yaml.ScalarNode { | ||
return api.ExpectedScalarAt(valNode) | ||
} | ||
if v, err := strconv.Atoi(valNode.Value); err != nil { | ||
return api.ExpectedIntAt(valNode) | ||
} else { | ||
s.Bar = v | ||
} | ||
default: | ||
if lo.Contains(api.BaseSpecFields, key) { | ||
continue | ||
} | ||
return api.UnknownFieldAt(key, keyNode) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
type Plugin struct{} | ||
|
||
func (p *Plugin) Info() api.PluginInfo { | ||
return api.PluginInfo{ | ||
Name: "bar", | ||
} | ||
} | ||
|
||
func (p *Plugin) Defaults() yaml.Unmarshaler { | ||
return &Defaults{} | ||
} | ||
|
||
func (p *Plugin) Specs() []api.Evaluable { | ||
return []api.Evaluable{&Spec{}} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
// Use and distribution licensed under the Apache license version 2. | ||
// | ||
// See the COPYING file in the root project directory for full text. | ||
|
||
package failer | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strconv" | ||
|
||
"github.com/gdt-dev/gdt/api" | ||
gdtapi "github.com/gdt-dev/gdt/api" | ||
"github.com/gdt-dev/gdt/plugin" | ||
"github.com/samber/lo" | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
func init() { | ||
plugin.Register(&Plugin{}) | ||
} | ||
|
||
type InnerDefaults struct { | ||
Fail bool `yaml:"fail,omitempty"` | ||
} | ||
|
||
type Defaults struct { | ||
InnerDefaults | ||
} | ||
|
||
func (d *Defaults) UnmarshalYAML(node *yaml.Node) error { | ||
if node.Kind != yaml.MappingNode { | ||
return api.ExpectedMapAt(node) | ||
} | ||
// maps/structs are stored in a top-level Node.Content field which is a | ||
// concatenated slice of Node pointers in pairs of key/values. | ||
for i := 0; i < len(node.Content); i += 2 { | ||
keyNode := node.Content[i] | ||
if keyNode.Kind != yaml.ScalarNode { | ||
return api.ExpectedScalarAt(keyNode) | ||
} | ||
key := keyNode.Value | ||
valNode := node.Content[i+1] | ||
switch key { | ||
case "fail": | ||
if valNode.Kind != yaml.MappingNode { | ||
return api.ExpectedMapAt(valNode) | ||
} | ||
inner := InnerDefaults{} | ||
if err := valNode.Decode(&inner); err != nil { | ||
return err | ||
} | ||
d.InnerDefaults = inner | ||
// This is just for testing api when parsing defaults... | ||
if d.Fail { | ||
return fmt.Errorf("defaults parsing failed") | ||
} | ||
default: | ||
continue | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
type Spec struct { | ||
api.Spec | ||
Fail bool `yaml:"fail"` | ||
} | ||
|
||
func (s *Spec) SetBase(b api.Spec) { | ||
s.Spec = b | ||
} | ||
|
||
func (s *Spec) Base() *api.Spec { | ||
return &s.Spec | ||
} | ||
|
||
func (s *Spec) Retry() *api.Retry { | ||
return nil | ||
} | ||
|
||
func (s *Spec) Timeout() *api.Timeout { | ||
return nil | ||
} | ||
|
||
func (s *Spec) Eval(context.Context) (*api.Result, error) { | ||
return nil, fmt.Errorf("%w: Indy, bad dates!", gdtapi.RuntimeError) | ||
} | ||
|
||
func (s *Spec) UnmarshalYAML(node *yaml.Node) error { | ||
if node.Kind != yaml.MappingNode { | ||
return api.ExpectedMapAt(node) | ||
} | ||
// maps/structs are stored in a top-level Node.Content field which is a | ||
// concatenated slice of Node pointers in pairs of key/values. | ||
for i := 0; i < len(node.Content); i += 2 { | ||
keyNode := node.Content[i] | ||
if keyNode.Kind != yaml.ScalarNode { | ||
return api.ExpectedScalarAt(keyNode) | ||
} | ||
key := keyNode.Value | ||
valNode := node.Content[i+1] | ||
switch key { | ||
case "fail": | ||
if valNode.Kind != yaml.ScalarNode { | ||
return api.ExpectedScalarAt(valNode) | ||
} | ||
s.Fail, _ = strconv.ParseBool(valNode.Value) | ||
if s.Fail { | ||
return fmt.Errorf("Indy, bad parse!") | ||
} | ||
default: | ||
if lo.Contains(api.BaseSpecFields, key) { | ||
continue | ||
} | ||
return api.UnknownFieldAt(key, keyNode) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
type Plugin struct{} | ||
|
||
func (p *Plugin) Info() api.PluginInfo { | ||
return api.PluginInfo{ | ||
Name: "fail", | ||
} | ||
} | ||
|
||
func (p *Plugin) Defaults() yaml.Unmarshaler { | ||
return &Defaults{} | ||
} | ||
|
||
func (p *Plugin) Specs() []api.Evaluable { | ||
return []api.Evaluable{&Spec{}} | ||
} |
Oops, something went wrong.