Skip to content

Commit 7c3980b

Browse files
author
Dean Karn
authored
Updates (#8)
* add default modifier tag * fix linting issues * add unhappy path tests
1 parent d9c7865 commit 7c3980b

File tree

12 files changed

+225
-48
lines changed

12 files changed

+225
-48
lines changed

.github/workflows/workflow.yml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
on:
2+
push:
3+
branches:
4+
- master
5+
pull_request:
6+
name: Test
7+
jobs:
8+
test:
9+
strategy:
10+
matrix:
11+
go-version: [1.14.x, 1.15.x]
12+
os: [ubuntu-latest, macos-latest, windows-latest]
13+
runs-on: ${{ matrix.os }}
14+
steps:
15+
- name: Install Go
16+
uses: actions/setup-go@v2
17+
with:
18+
go-version: ${{ matrix.go-version }}
19+
20+
- name: Checkout code
21+
uses: actions/checkout@v2
22+
23+
- name: Restore Cache
24+
uses: actions/cache@v2
25+
with:
26+
path: ~/go/pkg/mod
27+
key: ${{ runner.os }}-v1-go-${{ hashFiles('**/go.sum') }}
28+
restore-keys: |
29+
${{ runner.os }}-v1-go-
30+
31+
- name: Test
32+
run: go test -race -covermode=atomic -coverprofile="profile.cov" ./...
33+
34+
- name: Send Coverage
35+
if: matrix.os == 'ubuntu-latest' && matrix.go-version == '1.15.x'
36+
uses: shogo82148/actions-goveralls@v1
37+
with:
38+
path-to-profile: profile.cov
39+
40+
golangci:
41+
name: lint
42+
runs-on: ubuntu-latest
43+
steps:
44+
- uses: actions/checkout@v2
45+
- name: golangci-lint
46+
uses: golangci/golangci-lint-action@v2
47+
with:
48+
version: v1.32

.travis.yml

Lines changed: 0 additions & 34 deletions
This file was deleted.

Makefile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
GOCMD=GO111MODULE=on go
2+
3+
test:
4+
$(GOCMD) test -cover -race ./...
5+
6+
bench:
7+
$(GOCMD) test -bench=. -benchmem ./...
8+
9+
.PHONY: linters-install lint test bench

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package mold
22
============
3-
![Project status](https://img.shields.io/badge/version-3.0.1-green.svg)
3+
![Project status](https://img.shields.io/badge/version-3.1.0-green.svg)
44
[![Build Status](https://travis-ci.org/go-playground/mold.svg?branch=v2)](https://travis-ci.org/go-playground/mold)
55
[![Coverage Status](https://coveralls.io/repos/github/go-playground/mold/badge.svg?branch=v2)](https://coveralls.io/github/go-playground/mold?branch=v2)
66
[![Go Report Card](https://goreportcard.com/badge/github.com/go-playground/mold)](https://goreportcard.com/report/github.com/go-playground/mold)

cache.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@ const (
1616
typeEndKeys
1717
)
1818

19-
const (
20-
keysTagNotDefined = "'" + endKeysTag + "' tag encountered without a corresponding '" + keysTag + "' tag"
21-
)
22-
2319
type structCache struct {
2420
lock sync.Mutex
2521
m atomic.Value // map[reflect.Type]*cStruct
@@ -131,7 +127,7 @@ func (t *Transformer) extractStructCache(current reflect.Value) (*cStruct, error
131127
} else {
132128
// even if field doesn't have validations need cTag for traversing to potential inner/nested
133129
// elements of the field.
134-
ctag = new(cTag)
130+
ctag = &cTag{typeof: typeDefault}
135131
}
136132

137133
cs.fields = append(cs.fields, &cField{

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/go-playground/mold/v3
22

3-
go 1.13
3+
go 1.15
44

55
require (
66
github.com/go-playground/assert/v2 v2.0.1

modifiers/modifiers.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@ func New() *mold.Transformer {
1616
mod.Register("lcase", ToLower)
1717
mod.Register("ucase", ToUpper)
1818
mod.Register("snake", SnakeCase)
19+
mod.Register("default", Default)
1920
return mod
2021
}

modifiers/multi.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package modifiers
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"reflect"
7+
"strconv"
8+
"time"
9+
10+
"github.com/go-playground/mold/v3"
11+
)
12+
13+
//
14+
// Default allows setting of a default value IF no value is already present.
15+
//
16+
func Default(ctx context.Context, t *mold.Transformer, v reflect.Value, param string) error {
17+
if !v.IsZero() {
18+
return nil
19+
}
20+
21+
fmt.Println(param, v.Type())
22+
23+
switch v.Interface().(type) {
24+
case string:
25+
v.SetString(param)
26+
case int, int8, int16, int32, int64:
27+
value, err := strconv.Atoi(param)
28+
if err != nil {
29+
return err
30+
}
31+
v.SetInt(int64(value))
32+
33+
case uint, uint8, uint16, uint32, uint64:
34+
value, err := strconv.Atoi(param)
35+
if err != nil {
36+
return err
37+
}
38+
v.SetUint(uint64(value))
39+
case float32, float64:
40+
value, err := strconv.ParseFloat(param, 64)
41+
if err != nil {
42+
return err
43+
}
44+
v.SetFloat(value)
45+
case bool:
46+
value, err := strconv.ParseBool(param)
47+
if err != nil {
48+
return err
49+
}
50+
v.SetBool(value)
51+
case time.Duration:
52+
d, err := time.ParseDuration(param)
53+
if err != nil {
54+
return err
55+
}
56+
v.SetInt(int64(d))
57+
}
58+
return nil
59+
}

modifiers/multi_test.go

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package modifiers
2+
3+
import (
4+
"context"
5+
"testing"
6+
"time"
7+
8+
. "github.com/go-playground/assert/v2"
9+
)
10+
11+
func TestDefault(t *testing.T) {
12+
conform := New()
13+
14+
tests := []struct {
15+
name string
16+
field interface{}
17+
tags string
18+
expected interface{}
19+
expectError bool
20+
}{
21+
{
22+
name: "default string",
23+
field: "",
24+
tags: "default=test",
25+
expected: "test",
26+
},
27+
{
28+
name: "default int",
29+
field: 0,
30+
tags: "default=3",
31+
expected: 3,
32+
},
33+
{
34+
name: "default uint",
35+
field: uint(0),
36+
tags: "default=4",
37+
expected: uint(4),
38+
},
39+
{
40+
name: "default float",
41+
field: float32(0),
42+
tags: "default=5",
43+
expected: float32(5),
44+
},
45+
{
46+
name: "default bool",
47+
field: false,
48+
tags: "default=true",
49+
expected: true,
50+
},
51+
{
52+
name: "default time.Duration",
53+
field: time.Duration(0),
54+
tags: "default=1s",
55+
expected: time.Duration(1_000_000_000),
56+
},
57+
{
58+
name: "bad default time.Duration",
59+
field: time.Duration(0),
60+
tags: "default=rex",
61+
expectError: true,
62+
},
63+
{
64+
name: "bad default int",
65+
field: 0,
66+
tags: "default=abc",
67+
expectError: true,
68+
},
69+
{
70+
name: "bad default uint",
71+
field: uint(0),
72+
tags: "default=abc",
73+
expectError: true,
74+
},
75+
{
76+
name: "bad default float",
77+
field: float32(0),
78+
tags: "default=abc",
79+
expectError: true,
80+
},
81+
{
82+
name: "bad default bool",
83+
field: false,
84+
tags: "default=blue",
85+
expectError: true,
86+
},
87+
}
88+
89+
for _, tc := range tests {
90+
tc := tc
91+
t.Run(tc.name, func(t *testing.T) {
92+
t.Parallel()
93+
err := conform.Field(context.Background(), &tc.field, tc.tags)
94+
if tc.expectError {
95+
NotEqual(t, err, nil)
96+
return
97+
}
98+
Equal(t, err, nil)
99+
Equal(t, tc.field, tc.expected)
100+
})
101+
}
102+
}

mold.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,9 +242,6 @@ func (t *Transformer) setByField(ctx context.Context, orig reflect.Value, ct *cT
242242
if typ == timeType {
243243
return
244244
}
245-
if ct != nil {
246-
ct = ct.next
247-
}
248245

249246
if !current.CanAddr() {
250247
newVal := reflect.New(typ).Elem()

mold_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,8 @@ func TestBadValues(t *testing.T) {
2727
tform.Register("blah", func(ctx context.Context, t *Transformer, value reflect.Value, param string) error { return nil })
2828

2929
type Test struct {
30-
unexposed string
31-
Ignore string `mold:"-"`
32-
String string `mold:"blah,,blah"`
30+
Ignore string `mold:"-"`
31+
String string `mold:"blah,,blah"`
3332
}
3433

3534
var tt Test

scrubbers/util.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ import (
88

99
func hashString(input string) string {
1010
h := sha1.New()
11-
io.WriteString(h, input)
11+
_, _ = io.WriteString(h, input)
1212
return fmt.Sprintf("%x", h.Sum(nil))
1313
}

0 commit comments

Comments
 (0)