-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
doc: improved example to use the most recent version
- Loading branch information
Showing
8 changed files
with
212 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
func main() { | ||
fmt.Println("TODO: Write a more complex example") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package helper | ||
|
||
import "encoding/json" | ||
|
||
type ServerResponse struct { | ||
Status bool `json:"status"` | ||
Message string `json:"message"` | ||
Data json.RawMessage `json:"data,omitempty"` | ||
} | ||
|
||
func GenerateSuccessResponse(payload interface{}, message string) ([]byte, error) { | ||
data, err := json.Marshal(payload) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
s := &ServerResponse{ | ||
Status: true, | ||
Message: message, | ||
Data: data, | ||
} | ||
|
||
res, err := json.Marshal(s) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return res, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package v20230401 | ||
|
||
import ( | ||
"basicexample/helper" | ||
"encoding/json" | ||
"net/http" | ||
"strings" | ||
"time" | ||
) | ||
|
||
// Migrations | ||
type ListUserResponseMigration struct{} | ||
|
||
func (c *ListUserResponseMigration) Migrate( | ||
body []byte, | ||
h http.Header) ([]byte, http.Header, error) { | ||
type oldUser struct { | ||
UID string `json:"uid"` | ||
Email string `json:"email"` | ||
FullName string `json:"full_name"` | ||
Profile string `json:"profile"` | ||
CreatedAt time.Time `json:"created_at"` | ||
UpdatedAt time.Time `json:"updated_at"` | ||
} | ||
|
||
var res helper.ServerResponse | ||
err := json.Unmarshal(body, &res) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
var users []*oldUser20230501 | ||
err = json.Unmarshal(res.Data, &users) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
var newUsers []*oldUser | ||
for _, u := range users { | ||
var oldUser oldUser | ||
oldUser.UID = u.UID | ||
oldUser.Email = u.Email | ||
oldUser.FullName = strings.Join([]string{u.FirstName, u.LastName}, " ") | ||
oldUser.Profile = u.Profile | ||
oldUser.CreatedAt = u.CreatedAt | ||
oldUser.UpdatedAt = u.UpdatedAt | ||
newUsers = append(newUsers, &oldUser) | ||
} | ||
|
||
body, err = helper.GenerateSuccessResponse(&newUsers, "users retrieved successfully") | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
return body, h, nil | ||
} | ||
|
||
type oldUser20230501 struct { | ||
UID string `json:"uid"` | ||
Email string `json:"email"` | ||
FirstName string `json:"first_name"` | ||
LastName string `json:"last_name"` | ||
Profile string `json:"profile"` | ||
CreatedAt time.Time `json:"created_at"` | ||
UpdatedAt time.Time `json:"updated_at"` | ||
} |
Oops, something went wrong.