Skip to content

Commit beb0901

Browse files
authored
Merge pull request #3 from mh-cbon/master
close #3
2 parents 6e1ba8c + 544f26b commit beb0901

File tree

6 files changed

+168
-61
lines changed

6 files changed

+168
-61
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,6 @@ _testmain.go
2525
*.exe
2626
*.test
2727
*.prof
28+
29+
.glide/
30+
vendor/

.travis.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
language: go
3+
4+
go:
5+
- 1.4
6+
- 1.5
7+
- 1.6
8+
- 1.7
9+
- tip
10+
11+
before_install:
12+
- sudo apt-get -qq update
13+
- go get github.com/etgryphon/stringUp
14+
- go get github.com/icrowley/fake
15+
- go get github.com/stretchr/testify
16+
- go get github.com/davecgh/go-spew/spew
17+
- go get github.com/pmezard/go-difflib/difflib
18+
- mkdir -p ${GOPATH}/bin
19+
- cd ~
20+
- curl https://glide.sh/get | sh
21+
22+
install:
23+
- cd $GOPATH/src/github.com/${TRAVIS_REPO_SLUG}
24+
- glide install
25+
26+
script:
27+
- go test

conform.go

Lines changed: 54 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -163,54 +163,66 @@ func Strings(iface interface{}) error {
163163
v := ift.Field(i)
164164
el := reflect.Indirect(ifv.Elem().FieldByName(v.Name))
165165
switch el.Kind() {
166+
case reflect.Slice:
167+
if slice, ok := el.Interface().([]string); ok {
168+
for i, input := range slice {
169+
tags := v.Tag.Get("conform")
170+
slice[i] = transformString(input, tags)
171+
}
172+
return nil
173+
}
166174
case reflect.Struct:
167175
Strings(el.Addr().Interface())
168176
case reflect.String:
169177
if el.CanSet() {
170-
t := v.Tag.Get("conform")
171-
if t == "" {
172-
continue
173-
}
174-
d := el.String()
175-
for _, split := range strings.Split(t, ",") {
176-
switch split {
177-
case "trim":
178-
d = strings.TrimSpace(d)
179-
case "ltrim":
180-
d = strings.TrimLeft(d, " ")
181-
case "rtrim":
182-
d = strings.TrimRight(d, " ")
183-
case "lower":
184-
d = strings.ToLower(d)
185-
case "upper":
186-
d = strings.ToUpper(d)
187-
case "title":
188-
d = strings.Title(d)
189-
case "camel":
190-
d = stringUp.CamelCase(d)
191-
case "snake":
192-
d = camelTo(stringUp.CamelCase(d), "_")
193-
case "slug":
194-
d = camelTo(stringUp.CamelCase(d), "-")
195-
case "ucfirst":
196-
d = ucFirst(d)
197-
case "name":
198-
d = formatName(d)
199-
case "email":
200-
d = strings.ToLower(strings.TrimSpace(d))
201-
case "num":
202-
d = onlyNumbers(d)
203-
case "!num":
204-
d = stripNumbers(d)
205-
case "alpha":
206-
d = onlyAlpha(d)
207-
case "!alpha":
208-
d = stripAlpha(d)
209-
}
210-
}
211-
el.SetString(d)
178+
tags := v.Tag.Get("conform")
179+
input := el.String()
180+
el.SetString(transformString(input, tags))
212181
}
213182
}
214183
}
215184
return nil
216185
}
186+
187+
func transformString(input, tags string) string {
188+
if tags == "" {
189+
return input
190+
}
191+
for _, split := range strings.Split(tags, ",") {
192+
switch split {
193+
case "trim":
194+
input = strings.TrimSpace(input)
195+
case "ltrim":
196+
input = strings.TrimLeft(input, " ")
197+
case "rtrim":
198+
input = strings.TrimRight(input, " ")
199+
case "lower":
200+
input = strings.ToLower(input)
201+
case "upper":
202+
input = strings.ToUpper(input)
203+
case "title":
204+
input = strings.Title(input)
205+
case "camel":
206+
input = stringUp.CamelCase(input)
207+
case "snake":
208+
input = camelTo(stringUp.CamelCase(input), "_")
209+
case "slug":
210+
input = camelTo(stringUp.CamelCase(input), "-")
211+
case "ucfirst":
212+
input = ucFirst(input)
213+
case "name":
214+
input = formatName(input)
215+
case "email":
216+
input = strings.ToLower(strings.TrimSpace(input))
217+
case "num":
218+
input = onlyNumbers(input)
219+
case "!num":
220+
input = stripNumbers(input)
221+
case "alpha":
222+
input = onlyAlpha(input)
223+
case "!alpha":
224+
input = stripAlpha(input)
225+
}
226+
}
227+
return input
228+
}

conform_test.go

Lines changed: 52 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,29 @@ import (
1313
"github.com/stretchr/testify/suite"
1414
)
1515

16-
type testEmbeddedStruct struct {
16+
type TestEmbeddedStruct struct {
1717
FirstName string `conform:"name"`
1818
}
1919

20-
type testTwiceEmbeddedStruct struct {
21-
testEmbeddedStruct
20+
type TestTwiceEmbeddedStruct struct {
21+
TestEmbeddedStruct
2222
LastName string `conform:"name"`
2323
}
2424

25-
func (t *testTwiceEmbeddedStruct) private1() {}
26-
func (t *testTwiceEmbeddedStruct) private2() {}
27-
func (t *testTwiceEmbeddedStruct) Public1() {}
28-
func (t *testTwiceEmbeddedStruct) Public2() {}
25+
func (t *TestTwiceEmbeddedStruct) private1() {}
26+
func (t *TestTwiceEmbeddedStruct) private2() {}
27+
func (t *TestTwiceEmbeddedStruct) Public1() {}
28+
func (t *TestTwiceEmbeddedStruct) Public2() {}
2929

30-
type testThriceEmbeddedStruct struct {
31-
testTwiceEmbeddedStruct
30+
type TestThriceEmbeddedStruct struct {
31+
TestTwiceEmbeddedStruct
3232
Email string `conform:"email"`
3333
}
3434

35-
func (t *testThriceEmbeddedStruct) private1() {}
36-
func (t *testThriceEmbeddedStruct) private2() {}
37-
func (t *testThriceEmbeddedStruct) Public1() {}
38-
func (t *testThriceEmbeddedStruct) Public2() {}
35+
func (t *TestThriceEmbeddedStruct) private1() {}
36+
func (t *TestThriceEmbeddedStruct) private2() {}
37+
func (t *TestThriceEmbeddedStruct) Public1() {}
38+
func (t *TestThriceEmbeddedStruct) Public2() {}
3939

4040
type testSuite struct {
4141
suite.Suite
@@ -485,11 +485,11 @@ F:
485485

486486
}
487487

488-
func (t *testSuite) TestEmbeddedStruct() {
488+
func (t *testSuite) TestEmbeddedStructfn() {
489489
assert := assert.New(t.T())
490490

491491
var s struct {
492-
testEmbeddedStruct
492+
TestEmbeddedStruct
493493
LastName string `conform:"name"`
494494
}
495495

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

507-
func (t *testSuite) TestTwiceEmbeddedStruct() {
507+
func (t *testSuite) TestTwiceEmbeddedStructFn() {
508508
assert := assert.New(t.T())
509509

510510
var s struct {
511-
testTwiceEmbeddedStruct
511+
TestTwiceEmbeddedStruct
512512
Country string `conform:"trim,upper"`
513513
}
514514

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

529-
func (t *testSuite) TestThriceEmbeddedStruct() {
529+
func (t *testSuite) TestThriceEmbeddedStructFn() {
530530
assert := assert.New(t.T())
531531

532532
var s struct {
533-
testThriceEmbeddedStruct
533+
TestThriceEmbeddedStruct
534534
Country string `conform:"trim,upper"`
535535
}
536536

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

554+
func (t *testSuite) TestSlice() {
555+
assert := assert.New(t.T())
556+
557+
var s struct {
558+
Tags []string `conform:"trim"`
559+
}
560+
561+
s.Tags = append(s.Tags, " some")
562+
s.Tags = append(s.Tags, "string ")
563+
564+
Strings(&s)
565+
566+
assert.Equal("some", s.Tags[0], "tags[0] should be trimmed")
567+
assert.Equal("string", s.Tags[1], "tags[1] should be trimmed")
568+
}
569+
570+
func (t *testSuite) TestSliceOfSlice() {
571+
return /* @todo skip for now. */
572+
assert := assert.New(t.T())
573+
574+
var s struct {
575+
Tags [][]string `conform:"trim"`
576+
}
577+
578+
s.Tags = append(s.Tags, []string{" some ", "other "})
579+
s.Tags = append(s.Tags, []string{" string ", " beep "})
580+
581+
Strings(&s)
582+
583+
assert.Equal("some", s.Tags[0], "tags[0] should be trimmed")
584+
assert.Equal("string", s.Tags[1], "tags[1] should be trimmed")
585+
}
586+
554587
func TestStrings(t *testing.T) {
555588
suite.Run(t, new(testSuite))
556589
}

glide.lock

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

glide.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package: github.com/mh-cbon/conform
2+
import:
3+
- package: github.com/etgryphon/stringUp
4+
testImport:
5+
- package: github.com/icrowley/fake
6+
- package: github.com/stretchr/testify
7+
version: ^1.1.3
8+
subpackages:
9+
- assert
10+
- suite

0 commit comments

Comments
 (0)