Skip to content

Commit

Permalink
Update README to include info about simple type names
Browse files Browse the repository at this point in the history
  • Loading branch information
Kangaroux committed May 20, 2021
1 parent 460ae2a commit e7da8ad
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func main() {

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

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

`FieldMismatch.String()` returns a simple user friendly message describing what the issue is. You can of course use your own custom message instead.
`FieldMismatch.String()` returns a simple user friendly message describing what the issue is. You can of course use your own custom message instead.

## Universal Type Names

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.

For example, trying to convert a `float64` into an `int32` will report a mismatch between `float64` and `int32`.

If you don't want to include bit size, you can use `TypeNameSimple` which converts `floatX -> float`, `intX -> int`, `uintX -> uint`.

```go
opts := &schema.CompareOpts{
TypeNameFunc: schema.TypeNameSimple,
}

schema.CompareMapToStruct(dst, src, opts)
```

0 comments on commit e7da8ad

Please sign in to comment.