Skip to content

Commit f0a6470

Browse files
author
Dean Karn
authored
Updated Set logic + added Interceptors (#34)
1 parent a63be57 commit f0a6470

File tree

12 files changed

+270
-111
lines changed

12 files changed

+270
-111
lines changed

.github/workflows/workflow.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ jobs:
88
test:
99
strategy:
1010
matrix:
11-
go-version: [1.14.x, 1.15.x]
11+
go-version: [1.15.x, 1.16.x]
1212
os: [ubuntu-latest, macos-latest, windows-latest]
1313
runs-on: ${{ matrix.os }}
1414
steps:
@@ -32,7 +32,7 @@ jobs:
3232
run: go test -race -covermode=atomic -coverprofile="profile.cov" ./...
3333

3434
- name: Send Coverage
35-
if: matrix.os == 'ubuntu-latest' && matrix.go-version == '1.15.x'
35+
if: matrix.os == 'ubuntu-latest' && matrix.go-version == '1.16.x'
3636
uses: shogo82148/actions-goveralls@v1
3737
with:
3838
path-to-profile: profile.cov
@@ -45,4 +45,4 @@ jobs:
4545
- name: golangci-lint
4646
uses: golangci/golangci-lint-action@v2
4747
with:
48-
version: v1.32
48+
version: v1.41.1

Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
GOCMD=GO111MODULE=on go
22

3+
lint:
4+
golangci-lint run
5+
36
test:
47
$(GOCMD) test -cover -race ./...
58

69
bench:
710
$(GOCMD) test -bench=. -benchmem ./...
811

9-
.PHONY: linters-install lint test bench
12+
.PHONY: test bench lint

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-4.0.0-green.svg)
3+
![Project status](https://img.shields.io/badge/version-4.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)

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ require (
66
github.com/go-playground/assert/v2 v2.0.1
77
github.com/segmentio/go-camelcase v0.0.0-20160726192923-7085f1e3c734
88
github.com/segmentio/go-snakecase v1.2.0
9+
github.com/stretchr/testify v1.7.0
910
)

go.sum

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
1+
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
2+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
13
github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A=
24
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
5+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
6+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
37
github.com/segmentio/go-camelcase v0.0.0-20160726192923-7085f1e3c734 h1:Cpx2WLIv6fuPvaJAHNhYOgYzk/8RcJXu/8+mOrxf2KM=
48
github.com/segmentio/go-camelcase v0.0.0-20160726192923-7085f1e3c734/go.mod h1:hqVOMAwu+ekffC3Tvq5N1ljnXRrFKcaSjbCmQ8JgYaI=
59
github.com/segmentio/go-snakecase v1.2.0 h1:4cTmEjPGi03WmyAHWBjX53viTpBkn/z+4DO++fqYvpw=
610
github.com/segmentio/go-snakecase v1.2.0/go.mod h1:jk1miR5MS7Na32PZUykG89Arm+1BUSYhuGR6b7+hJto=
11+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
12+
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
13+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
14+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
15+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
16+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
17+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

modifiers/multi.go

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,17 @@ package modifiers
22

33
import (
44
"context"
5+
"reflect"
56
"strconv"
67
"time"
78

89
"github.com/go-playground/mold/v4"
910
)
1011

12+
var (
13+
durationType = reflect.TypeOf(time.Duration(0))
14+
)
15+
1116
//
1217
// defaultValue allows setting of a default value IF no value is already present.
1318
//
@@ -16,44 +21,56 @@ func defaultValue(ctx context.Context, fl mold.FieldLevel) error {
1621
return nil
1722
}
1823

19-
switch fl.Field().Interface().(type) {
20-
case string:
24+
switch fl.Field().Kind() {
25+
case reflect.String:
2126
fl.Field().SetString(fl.Param())
2227

23-
case int, int8, int16, int32, int64:
28+
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32:
2429
value, err := strconv.Atoi(fl.Param())
2530
if err != nil {
2631
return err
2732
}
2833
fl.Field().SetInt(int64(value))
2934

30-
case uint, uint8, uint16, uint32, uint64:
35+
case reflect.Int64:
36+
var value int64
37+
38+
if fl.Field().Type() == durationType {
39+
d, err := time.ParseDuration(fl.Param())
40+
if err != nil {
41+
return err
42+
}
43+
value = int64(d)
44+
} else {
45+
i, err := strconv.Atoi(fl.Param())
46+
if err != nil {
47+
return err
48+
}
49+
value = int64(i)
50+
}
51+
fl.Field().SetInt(value)
52+
53+
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
3154
value, err := strconv.Atoi(fl.Param())
3255
if err != nil {
3356
return err
3457
}
3558
fl.Field().SetUint(uint64(value))
3659

37-
case float32, float64:
60+
case reflect.Float32, reflect.Float64:
3861
value, err := strconv.ParseFloat(fl.Param(), 64)
3962
if err != nil {
4063
return err
4164
}
4265
fl.Field().SetFloat(value)
4366

44-
case bool:
67+
case reflect.Bool:
4568
value, err := strconv.ParseBool(fl.Param())
4669
if err != nil {
4770
return err
4871
}
4972
fl.Field().SetBool(value)
5073

51-
case time.Duration:
52-
d, err := time.ParseDuration(fl.Param())
53-
if err != nil {
54-
return err
55-
}
56-
fl.Field().SetInt(int64(d))
5774
}
5875
return nil
5976
}

modifiers/multi_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ import (
99
)
1010

1111
func TestDefault(t *testing.T) {
12+
13+
type State int
14+
const FINISHED State = 5
15+
16+
var state State
17+
1218
conform := New()
1319

1420
tests := []struct {
@@ -18,6 +24,12 @@ func TestDefault(t *testing.T) {
1824
expected interface{}
1925
expectError bool
2026
}{
27+
{
28+
name: "default State (although enum default value should be the default in practice)",
29+
field: state,
30+
tags: "default=5",
31+
expected: FINISHED,
32+
},
2133
{
2234
name: "default string",
2335
field: "",

0 commit comments

Comments
 (0)