Skip to content

Commit 3acdfd9

Browse files
author
Dean Karn
authored
fix current tracking for unadressable data (#40)
1 parent 9a50f08 commit 3acdfd9

File tree

7 files changed

+37
-17
lines changed

7 files changed

+37
-17
lines changed

.github/workflows/workflow.yml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,20 @@ jobs:
88
test:
99
strategy:
1010
matrix:
11-
go-version: [1.15.x, 1.16.x]
11+
go-version: [1.20.x, 1.19.x, 1.18.x]
1212
os: [ubuntu-latest, macos-latest, windows-latest]
1313
runs-on: ${{ matrix.os }}
1414
steps:
1515
- name: Install Go
16-
uses: actions/setup-go@v2
16+
uses: actions/setup-go@v3
1717
with:
1818
go-version: ${{ matrix.go-version }}
1919

2020
- name: Checkout code
21-
uses: actions/checkout@v2
21+
uses: actions/checkout@v3
2222

2323
- name: Restore Cache
24-
uses: actions/cache@v2
24+
uses: actions/cache@v3
2525
with:
2626
path: ~/go/pkg/mod
2727
key: ${{ runner.os }}-v1-go-${{ hashFiles('**/go.sum') }}
@@ -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.16.x'
35+
if: matrix.os == 'ubuntu-latest' && matrix.go-version == '1.20.x'
3636
uses: shogo82148/actions-goveralls@v1
3737
with:
3838
path-to-profile: profile.cov
@@ -41,8 +41,8 @@ jobs:
4141
name: lint
4242
runs-on: ubuntu-latest
4343
steps:
44-
- uses: actions/checkout@v2
44+
- uses: actions/checkout@v3
4545
- name: golangci-lint
4646
uses: golangci/golangci-lint-action@v2
4747
with:
48-
version: v1.41.1
48+
version: latest

README.md

Lines changed: 5 additions & 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.2.0-green.svg)
3+
![Project status](https://img.shields.io/badge/version-4.2.1-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)
@@ -11,6 +11,10 @@ Package mold is a general library to help modify or set data within data structu
1111

1212
How can this help me you ask, please see the examples [here](_examples/full/main.go)
1313

14+
Requirements
15+
------------
16+
- Go 1.18+
17+
1418
Installation
1519
------------
1620

go.mod

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
module github.com/go-playground/mold/v4
22

3-
go 1.15
3+
go 1.18
44

55
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
99
github.com/stretchr/testify v1.7.0
10+
golang.org/x/text v0.6.0
11+
)
12+
13+
require (
14+
github.com/davecgh/go-spew v1.1.0 // indirect
15+
github.com/pmezard/go-difflib v1.0.0 // indirect
16+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
1017
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ github.com/segmentio/go-snakecase v1.2.0/go.mod h1:jk1miR5MS7Na32PZUykG89Arm+1BU
1111
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
1212
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
1313
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
14+
golang.org/x/text v0.6.0 h1:3XmdazWV+ubf7QgHSTWeykHOci5oeekaGJBLkrkaw4k=
15+
golang.org/x/text v0.6.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
1416
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
1517
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
1618
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=

modifiers/string.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package modifiers
33
import (
44
"bytes"
55
"context"
6+
"golang.org/x/text/cases"
7+
"golang.org/x/text/language"
68
"reflect"
79
"regexp"
810
"strings"
@@ -90,7 +92,7 @@ func snakeCase(ctx context.Context, fl mold.FieldLevel) error {
9092
func titleCase(ctx context.Context, fl mold.FieldLevel) error {
9193
switch fl.Field().Kind() {
9294
case reflect.String:
93-
fl.Field().SetString(strings.Title(fl.Field().String()))
95+
fl.Field().SetString(cases.Title(language.Und, cases.NoLower).String(fl.Field().String()))
9496
}
9597
return nil
9698
}
@@ -112,7 +114,7 @@ var nameRegex = regexp.MustCompile(`[\p{L}]([\p{L}|[:space:]\-']*[\p{L}])*`)
112114
func nameCase(ctx context.Context, fl mold.FieldLevel) error {
113115
switch fl.Field().Kind() {
114116
case reflect.String:
115-
fl.Field().SetString(strings.Title(nameRegex.FindString(onlyOne(strings.ToLower(fl.Field().String())))))
117+
fl.Field().SetString(cases.Title(language.Und, cases.NoLower).String(nameRegex.FindString(onlyOne(strings.ToLower(fl.Field().String())))))
116118
}
117119
return nil
118120
}

modifiers/string_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@ import (
2222
// go test -coverprofile cover.out && go tool cover -html=cover.out -o cover.html
2323
//
2424

25+
func TestMultiple(t *testing.T) {
26+
assert := require.New(t)
27+
conform := New()
28+
s := interface{}("JOEYBLOGGS ")
29+
30+
err := conform.Field(context.Background(), &s, "trim,lcase")
31+
assert.NoError(err)
32+
assert.Equal("joeybloggs", s)
33+
}
34+
2535
func TestEnumType(t *testing.T) {
2636
assert := require.New(t)
2737

mold.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,14 @@ type Transform interface {
2525
// Func defines a transform function for use.
2626
type Func func(ctx context.Context, fl FieldLevel) error
2727

28-
//
2928
// StructLevelFunc accepts all values needed for struct level manipulation.
3029
//
3130
// Why does this exist? For structs for which you may not have access or rights to add tags too,
3231
// from other packages your using.
33-
//
3432
type StructLevelFunc func(ctx context.Context, sl StructLevel) error
3533

36-
//
3734
// InterceptorFunc is a way to intercept custom types to redirect the functions to be applied to an inner typ/value.
3835
// eg. sql.NullString, the manipulation should be done on the inner string.
39-
//
4036
type InterceptorFunc func(current reflect.Value) (inner reflect.Value)
4137

4238
// Transformer is the base controlling object which contains
@@ -135,13 +131,11 @@ func (t *Transformer) RegisterStructLevel(fn StructLevelFunc, types ...interface
135131
}
136132
}
137133

138-
//
139134
// RegisterInterceptor registers a new interceptor functions agains one or more types.
140135
// This InterceptorFunc allows one to intercept the incoming to to redirect the application of modifications
141136
// to an inner type/value.
142137
//
143138
// eg. sql.NullString
144-
//
145139
func (t *Transformer) RegisterInterceptor(fn InterceptorFunc, types ...interface{}) {
146140
for _, typ := range types {
147141
t.interceptors[reflect.TypeOf(typ)] = fn
@@ -263,6 +257,7 @@ func (t *Transformer) setByField(ctx context.Context, orig reflect.Value, ct *cT
263257
return
264258
}
265259
orig.Set(reflect.Indirect(newVal))
260+
current, kind = t.extractType(orig)
266261
} else {
267262
if err = ct.fn(ctx, fieldLevel{
268263
transformer: t,

0 commit comments

Comments
 (0)