Skip to content

Commit

Permalink
Better transformation guide
Browse files Browse the repository at this point in the history
  • Loading branch information
EwenQuim committed Jul 8, 2024
1 parent e1f039e commit ea42a7b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
39 changes: 38 additions & 1 deletion documentation/docs/guides/transformation.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ In the example above, we have a `User` struct with two fields: `FirstName` and `
When using the following controller, the data will be transformed before it is unmarshaled into the `User` struct.

```go
func myController(c fuego.ContextWithBody[User]) (User, error) {
func echoCapitalized(c fuego.ContextWithBody[User]) (User, error) {
user, err := c.Body()
if err != nil {
return User{}, err
Expand All @@ -53,3 +53,40 @@ func myController(c fuego.ContextWithBody[User]) (User, error) {
return u, nil
}
```

## Recursion

Transformation is not recursive. If you have nested structs, you will need to transform each struct individually. This is done on purpose to give you more control over the transformation process.

```go
type Address struct {
Street string `json:"street"`
City string `json:"city"`
}

func (a *Address) InTransform(ctx context.Context) error {
a.Street = strings.TrimSpace(a.Street)
a.City = strings.ToUpper(a.City)
return nil
}

type User struct {
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
// highlight-next-line
Address Address `json:"address"` // Nested struct
}

func (u *User) InTransform(ctx context.Context) error {
u.FirstName = strings.ToUpper(u.FirstName)
u.LastName = strings.TrimSpace(u.LastName)

// highlight-next-line
err := u.Address.InTransform(ctx) // Transform the nested struct
if err != nil {
return err
}

return nil
}
```
1 change: 1 addition & 0 deletions documentation/src/components/FlowChart.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ subgraph fuego
click ErrorHandler "/fuego/docs/guides/errors" "Error Handling"
end
ErrorHandler -- JSON{b:'Error!'} --> resp(Response)
Serialization -- JSON{b:'My Response!'} --> resp(Response)
Controller -. JSON{b:'Custom Response!'} .-> resp(Response)
Expand Down

0 comments on commit ea42a7b

Please sign in to comment.