Skip to content

Commit

Permalink
Merge pull request #3 from mh-cbon/master
Browse files Browse the repository at this point in the history
close #3
  • Loading branch information
leebenson authored Aug 27, 2016
2 parents 6e1ba8c + 544f26b commit beb0901
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 61 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@ _testmain.go
*.exe
*.test
*.prof

.glide/
vendor/
27 changes: 27 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

language: go

go:
- 1.4
- 1.5
- 1.6
- 1.7
- tip

before_install:
- sudo apt-get -qq update
- go get github.com/etgryphon/stringUp
- go get github.com/icrowley/fake
- go get github.com/stretchr/testify
- go get github.com/davecgh/go-spew/spew
- go get github.com/pmezard/go-difflib/difflib
- mkdir -p ${GOPATH}/bin
- cd ~
- curl https://glide.sh/get | sh

install:
- cd $GOPATH/src/github.com/${TRAVIS_REPO_SLUG}
- glide install

script:
- go test
96 changes: 54 additions & 42 deletions conform.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,54 +163,66 @@ func Strings(iface interface{}) error {
v := ift.Field(i)
el := reflect.Indirect(ifv.Elem().FieldByName(v.Name))
switch el.Kind() {
case reflect.Slice:
if slice, ok := el.Interface().([]string); ok {
for i, input := range slice {
tags := v.Tag.Get("conform")
slice[i] = transformString(input, tags)
}
return nil
}
case reflect.Struct:
Strings(el.Addr().Interface())
case reflect.String:
if el.CanSet() {
t := v.Tag.Get("conform")
if t == "" {
continue
}
d := el.String()
for _, split := range strings.Split(t, ",") {
switch split {
case "trim":
d = strings.TrimSpace(d)
case "ltrim":
d = strings.TrimLeft(d, " ")
case "rtrim":
d = strings.TrimRight(d, " ")
case "lower":
d = strings.ToLower(d)
case "upper":
d = strings.ToUpper(d)
case "title":
d = strings.Title(d)
case "camel":
d = stringUp.CamelCase(d)
case "snake":
d = camelTo(stringUp.CamelCase(d), "_")
case "slug":
d = camelTo(stringUp.CamelCase(d), "-")
case "ucfirst":
d = ucFirst(d)
case "name":
d = formatName(d)
case "email":
d = strings.ToLower(strings.TrimSpace(d))
case "num":
d = onlyNumbers(d)
case "!num":
d = stripNumbers(d)
case "alpha":
d = onlyAlpha(d)
case "!alpha":
d = stripAlpha(d)
}
}
el.SetString(d)
tags := v.Tag.Get("conform")
input := el.String()
el.SetString(transformString(input, tags))
}
}
}
return nil
}

func transformString(input, tags string) string {
if tags == "" {
return input
}
for _, split := range strings.Split(tags, ",") {
switch split {
case "trim":
input = strings.TrimSpace(input)
case "ltrim":
input = strings.TrimLeft(input, " ")
case "rtrim":
input = strings.TrimRight(input, " ")
case "lower":
input = strings.ToLower(input)
case "upper":
input = strings.ToUpper(input)
case "title":
input = strings.Title(input)
case "camel":
input = stringUp.CamelCase(input)
case "snake":
input = camelTo(stringUp.CamelCase(input), "_")
case "slug":
input = camelTo(stringUp.CamelCase(input), "-")
case "ucfirst":
input = ucFirst(input)
case "name":
input = formatName(input)
case "email":
input = strings.ToLower(strings.TrimSpace(input))
case "num":
input = onlyNumbers(input)
case "!num":
input = stripNumbers(input)
case "alpha":
input = onlyAlpha(input)
case "!alpha":
input = stripAlpha(input)
}
}
return input
}
71 changes: 52 additions & 19 deletions conform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,29 @@ import (
"github.com/stretchr/testify/suite"
)

type testEmbeddedStruct struct {
type TestEmbeddedStruct struct {
FirstName string `conform:"name"`
}

type testTwiceEmbeddedStruct struct {
testEmbeddedStruct
type TestTwiceEmbeddedStruct struct {
TestEmbeddedStruct
LastName string `conform:"name"`
}

func (t *testTwiceEmbeddedStruct) private1() {}
func (t *testTwiceEmbeddedStruct) private2() {}
func (t *testTwiceEmbeddedStruct) Public1() {}
func (t *testTwiceEmbeddedStruct) Public2() {}
func (t *TestTwiceEmbeddedStruct) private1() {}
func (t *TestTwiceEmbeddedStruct) private2() {}
func (t *TestTwiceEmbeddedStruct) Public1() {}
func (t *TestTwiceEmbeddedStruct) Public2() {}

type testThriceEmbeddedStruct struct {
testTwiceEmbeddedStruct
type TestThriceEmbeddedStruct struct {
TestTwiceEmbeddedStruct
Email string `conform:"email"`
}

func (t *testThriceEmbeddedStruct) private1() {}
func (t *testThriceEmbeddedStruct) private2() {}
func (t *testThriceEmbeddedStruct) Public1() {}
func (t *testThriceEmbeddedStruct) Public2() {}
func (t *TestThriceEmbeddedStruct) private1() {}
func (t *TestThriceEmbeddedStruct) private2() {}
func (t *TestThriceEmbeddedStruct) Public1() {}
func (t *TestThriceEmbeddedStruct) Public2() {}

type testSuite struct {
suite.Suite
Expand Down Expand Up @@ -485,11 +485,11 @@ F:

}

func (t *testSuite) TestEmbeddedStruct() {
func (t *testSuite) TestEmbeddedStructfn() {
assert := assert.New(t.T())

var s struct {
testEmbeddedStruct
TestEmbeddedStruct
LastName string `conform:"name"`
}

Expand All @@ -504,11 +504,11 @@ func (t *testSuite) TestEmbeddedStruct() {
assert.Equal(ln, s.LastName, "Last name should be stripped of numbers")
}

func (t *testSuite) TestTwiceEmbeddedStruct() {
func (t *testSuite) TestTwiceEmbeddedStructFn() {
assert := assert.New(t.T())

var s struct {
testTwiceEmbeddedStruct
TestTwiceEmbeddedStruct
Country string `conform:"trim,upper"`
}

Expand All @@ -526,11 +526,11 @@ func (t *testSuite) TestTwiceEmbeddedStruct() {
assert.Equal(s.Country, "UNITED KINGDOM", "Last name should be stripped of numbers")
}

func (t *testSuite) TestThriceEmbeddedStruct() {
func (t *testSuite) TestThriceEmbeddedStructFn() {
assert := assert.New(t.T())

var s struct {
testThriceEmbeddedStruct
TestThriceEmbeddedStruct
Country string `conform:"trim,upper"`
}

Expand All @@ -551,6 +551,39 @@ func (t *testSuite) TestThriceEmbeddedStruct() {
assert.Equal(s.Country, "UNITED KINGDOM", "Last name should be stripped of numbers")
}

func (t *testSuite) TestSlice() {
assert := assert.New(t.T())

var s struct {
Tags []string `conform:"trim"`
}

s.Tags = append(s.Tags, " some")
s.Tags = append(s.Tags, "string ")

Strings(&s)

assert.Equal("some", s.Tags[0], "tags[0] should be trimmed")
assert.Equal("string", s.Tags[1], "tags[1] should be trimmed")
}

func (t *testSuite) TestSliceOfSlice() {
return /* @todo skip for now. */
assert := assert.New(t.T())

var s struct {
Tags [][]string `conform:"trim"`
}

s.Tags = append(s.Tags, []string{" some ", "other "})
s.Tags = append(s.Tags, []string{" string ", " beep "})

Strings(&s)

assert.Equal("some", s.Tags[0], "tags[0] should be trimmed")
assert.Equal("string", s.Tags[1], "tags[1] should be trimmed")
}

func TestStrings(t *testing.T) {
suite.Run(t, new(testSuite))
}
22 changes: 22 additions & 0 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions glide.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package: github.com/mh-cbon/conform
import:
- package: github.com/etgryphon/stringUp
testImport:
- package: github.com/icrowley/fake
- package: github.com/stretchr/testify
version: ^1.1.3
subpackages:
- assert
- suite

0 comments on commit beb0901

Please sign in to comment.