Skip to content

Commit

Permalink
Merge pull request #19 from segmentio/add-data-manipulation
Browse files Browse the repository at this point in the history
Add mod tag an allow modification of the data
  • Loading branch information
deankarn committed Nov 4, 2017
2 parents 15618bf + 94399ba commit c52a746
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
14 changes: 14 additions & 0 deletions load.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package conf

import (
"bytes"
"context"
"errors"
"flag"
"fmt"
Expand All @@ -12,13 +13,22 @@ import (
"sort"
"strings"

"gopkg.in/go-playground/mold.v2/modifiers"

validator "gopkg.in/validator.v2"

// Load all default adapters of the objconv package.
_ "github.com/segmentio/objconv/adapters"
"github.com/segmentio/objconv/yaml"
)

var (
// Modifier is the default modification lib using the "mod" tag; it is
// exposed to allow registering of custom modifiers and aliases or to
// be set to a more central instance located in another repo.
Modifier = modifiers.New()
)

// Load the program's configuration into cfg, and returns the list of leftover
// arguments.
//
Expand Down Expand Up @@ -139,6 +149,10 @@ func (ld Loader) Load(cfg interface{}) (cmd string, args []string, err error) {
return
}

if err = Modifier.Struct(context.Background(), cfg); err != nil {
return
}

if err = validator.Validate(v.Interface()); err != nil {
err = makeValidationError(err, v.Type())
}
Expand Down
16 changes: 16 additions & 0 deletions load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,22 @@ func TestValidator(t *testing.T) {
}
}

func TestModifiers(t *testing.T) {
config := struct {
Email string `conf:"email" validate:"nonzero" mod:"trim,lcase"`
}{
Email: " [email protected]",
}

_, _, err := (Loader{}).Load(&config)
if err != nil {
t.Error("bad error:", err)
}
if config.Email != "[email protected]" {
t.Error("bad mod value:", config.Email)
}
}

func parseURL(s string) url.URL {
u, _ := url.Parse(s)
return *u
Expand Down

0 comments on commit c52a746

Please sign in to comment.