Skip to content

Commit 0b37c2e

Browse files
authored
Merge pull request #4 from Osendo/pr
feat: add ignore struct tag
2 parents 75056be + 8c20434 commit 0b37c2e

File tree

4 files changed

+45
-0
lines changed

4 files changed

+45
-0
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,23 @@ type UserDto struct {
7979
}
8080
```
8181

82+
##### Ignored fields
83+
84+
If you need to ignore any of the structure fields, you can apply the structure tag - dto:ignore
85+
86+
```go
87+
type Order struct {
88+
Id string
89+
90+
json.Marshaler `dto:"ignore"`
91+
json.Unmarshaler `dto:"ignore"`
92+
}
93+
94+
type OrderDto struct {
95+
Id string
96+
}
97+
```
98+
8299
#### Mapper instances
83100

84101
Local mapper instances can be used to add conversion and inspection functions. Mappers don't change their internal state during mapping, so they can be reused at any time.

dto.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"errors"
1818
"fmt"
1919
"reflect"
20+
"strings"
2021
)
2122

2223
type structValueMap = map[string]reflect.Value
@@ -31,6 +32,8 @@ var mapperPtrRfType = reflect.TypeOf((*Mapper)(nil))
3132
type convertFuncClosure = func(reflect.Value, *Mapper) (reflect.Value, error)
3233
type inspectFuncClosure = func(reflect.Value, reflect.Value, *Mapper) error
3334

35+
const structTag = "dto"
36+
3437
// NoValidMappingError indicates that no valid mapping was found
3538
type NoValidMappingError struct {
3639
ToType reflect.Type
@@ -55,6 +58,11 @@ func collectStructFields(rfValue reflect.Value, rfType reflect.Type, fields stru
5558
for i := 0; i < rfType.NumField(); i++ {
5659
fieldValue := rfValue.Field(i)
5760
fieldType := rfType.Field(i)
61+
if tags, ok := fieldType.Tag.Lookup(structTag); ok {
62+
if strings.Contains(tags, "ignore") {
63+
continue
64+
}
65+
}
5866
if fieldType.Anonymous {
5967
collectStructFields(fieldValue, fieldType.Type, fields)
6068
} else {

dto_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package dto
22

33
import (
4+
"encoding/json"
45
"errors"
56
"fmt"
67
"reflect"
@@ -37,6 +38,17 @@ type User struct {
3738
Password RawPassword
3839
}
3940

41+
type Order struct {
42+
Id string
43+
44+
json.Marshaler `dto:"ignore"`
45+
json.Unmarshaler `dto:"ignore"`
46+
}
47+
48+
type OrderDto struct {
49+
Id string
50+
}
51+
4052
// ==================================== Data for tests ========================
4153

4254
var commonProducts = []Product{
@@ -346,6 +358,13 @@ func TestPointerCases(t *testing.T) {
346358
}
347359
}
348360

361+
func TestStructureTagIgnoreCase(t *testing.T) {
362+
order := Order{Id: "test"}
363+
var outOrder OrderDto
364+
Map(&outOrder, order)
365+
assert.Equal(t, order.Id, outOrder.Id)
366+
}
367+
349368
// ==================================== Benchmarks ============================
350369

351370
type benchCart = struct {

go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
55
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
66
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
77
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
8+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
89
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
910
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
1011
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 commit comments

Comments
 (0)