Skip to content

Commit

Permalink
doc: added writing migrations guide
Browse files Browse the repository at this point in the history
  • Loading branch information
subomi committed Jan 16, 2024
1 parent 39aa7ef commit 69f848e
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,39 @@ func createUser(r *http.Request, w http.ResponseWriter) {

```

### Writing migrations
A migration is a struct that performs a migration on either a request or a response, but not both. Here's an example:

```go
type createUserRequestSplitNameMigration struct{}

func (c *createUserRequestSplitNameMigration) Migrate(body []byte, h http.Header) ([]byte, http.Header, error) {
var oUser oldUser
err := json.Unmarshal(body, &oUser)
if err != nil {
return nil, nil, err
}

var nUser user
nUser.Email = oUser.Email

splitName := strings.Split(oUser.FullName, " ")
nUser.FirstName = splitName[0]
nUser.LastName = splitName[1]

body, err = json.Marshal(&nUser)
if err != nil {
return nil, nil, err
}

return body, h, nil
}
```

Notice from the above that the migration struct name follows a particular structure. The structure adopted is `{handlerName}{MigrationType}`. The `handlerName` refers to the exact name of your handler. For example, if you have a handler named `LoginUser`, any migration on this handler should start with `LoginUser`. It'll also be what we use in `VersionRequest` and `VersionResponse`. The `MigrationType` can be `Request` or `Response`. We use this field to determine if the migration should run on the request or the response payload.

This library doesn't support multiple transformations per version as of the time of this writing. For example, no handler can have multiple changes for each version update.


## License
MIT License

0 comments on commit 69f848e

Please sign in to comment.