Skip to content

Commit

Permalink
Making a start on the YAML form builder
Browse files Browse the repository at this point in the history
  • Loading branch information
Danzabar committed Nov 5, 2015
1 parent 5cdfe50 commit c359f56
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
47 changes: 47 additions & 0 deletions builder.go
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
}
12 changes: 12 additions & 0 deletions builder_test.go
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)
}
4 changes: 4 additions & 0 deletions example.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Name: Test
Description: Test
Action: test
Method: POST

0 comments on commit c359f56

Please sign in to comment.