Skip to content

Commit e7da8ad

Browse files
committed
Update README to include info about simple type names
1 parent 460ae2a commit e7da8ad

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

README.md

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func main() {
8282

8383
// The struct we want to load the payload into (dst).
8484
p := Person{}
85-
results, _ := schema.CompareMapToStruct(&p, m)
85+
results, _ := schema.CompareMapToStruct(&p, m, nil)
8686

8787
fmt.Println("missing fields: ", results.MissingFields)
8888
fmt.Println("mismatched fields:", results.MismatchedFields)
@@ -96,4 +96,20 @@ missing fields: [last_name]
9696
mismatched fields: [expected "age" to be a int but it's a string]
9797
```
9898

99-
`FieldMismatch.String()` returns a simple user friendly message describing what the issue is. You can of course use your own custom message instead.
99+
`FieldMismatch.String()` returns a simple user friendly message describing what the issue is. You can of course use your own custom message instead.
100+
101+
## Universal Type Names
102+
103+
By default, `CompareMapToStruct` will use the `TypeNameDetailed` func when reporting a type mismatch. The detailed type name includes some extra information that you may not want a client to see.
104+
105+
For example, trying to convert a `float64` into an `int32` will report a mismatch between `float64` and `int32`.
106+
107+
If you don't want to include bit size, you can use `TypeNameSimple` which converts `floatX -> float`, `intX -> int`, `uintX -> uint`.
108+
109+
```go
110+
opts := &schema.CompareOpts{
111+
TypeNameFunc: schema.TypeNameSimple,
112+
}
113+
114+
schema.CompareMapToStruct(dst, src, opts)
115+
```

0 commit comments

Comments
 (0)