-
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.
Making a start on the YAML form builder
- Loading branch information
Showing
3 changed files
with
63 additions
and
0 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,47 @@ | ||
package forms | ||
|
||
import ( | ||
"gopkg.in/yaml.v2" | ||
"io/ioutil" | ||
"path/filepath" | ||
) | ||
|
||
// The builder struct | ||
type Builder struct { | ||
// The location of the YAML file | ||
Filename string | ||
// The extracted configuration settings | ||
Config Config | ||
// The form struct that will be populated from the yaml file | ||
Form *Form | ||
} | ||
|
||
// The YAML config struct | ||
type Config struct { | ||
Name string `yaml:"Name"` | ||
Description string `yaml:"Description"` | ||
Action string `yaml:"Action"` | ||
Method string `yaml:"Method"` | ||
} | ||
|
||
func NewBuilder(file string) *Builder { | ||
path, _ := filepath.Abs(file) | ||
|
||
b := &Builder{ | ||
Filename: path, | ||
} | ||
// Extract settings | ||
b.extract() | ||
|
||
return b | ||
} | ||
|
||
func (b *Builder) extract() { | ||
data, _ := ioutil.ReadFile(b.Filename) | ||
|
||
var c Config | ||
|
||
yaml.Unmarshal([]byte(data), &c) | ||
|
||
b.Config = c | ||
} |
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,12 @@ | ||
package forms | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestParseYamlFile(t *testing.T) { | ||
b := NewBuilder("example.yml") | ||
|
||
assert.Equal(t, "Test", b.Config.Name) | ||
} |
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,4 @@ | ||
Name: Test | ||
Description: Test | ||
Action: test | ||
Method: POST |