-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
142 lines (110 loc) · 3.97 KB
/
main.go
File metadata and controls
142 lines (110 loc) · 3.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Package main demonstrates API response formatting.
package main
import (
"encoding/json"
"errors"
"fmt"
"github.com/yigithankarabulut/wirekit"
"github.com/yigithankarabulut/wirekit/response"
)
// User represents a user in the system.
type User struct {
ID int `json:"id"`
Name string `json:"name"`
Email string `json:"email"`
}
func printJSON(label string, v any) {
data, _ := json.MarshalIndent(v, " ", " ")
fmt.Printf("%s\n %s\n", label, string(data))
}
// SuccessResponses demonstrates success response types.
func SuccessResponses() {
fmt.Println("\n--- Success Responses ---")
// Simple success with data
user := User{ID: 1, Name: "John Doe", Email: "john@example.com"}
successResp := wirekit.Response.Success(user)
printJSON("Success:", successResp)
// Success with message only
msgResp := wirekit.Response.SuccessMessage("Operation completed successfully")
printJSON("Success Message:", msgResp)
// Created response
newUser := User{ID: 2, Name: "Jane Doe", Email: "jane@example.com"}
createdResp := wirekit.Response.Created(newUser)
printJSON("Created:", createdResp)
// No content response
noContentResp := wirekit.Response.NoContent()
printJSON("No Content:", noContentResp)
}
// ErrorResponses demonstrates error response types.
func ErrorResponses() {
fmt.Println("\n--- Error Responses ---")
// Fail with error
err := errors.New("something went wrong")
failResp := wirekit.Response.Fail(err)
printJSON("Fail:", failResp)
// Fail with status code
codeResp := wirekit.Response.FailWithCode(404)
printJSON("Fail with Code:", codeResp)
// Fail with message
msgFailResp := wirekit.Response.FailWithMessage("Invalid input provided")
printJSON("Fail with Message:", msgFailResp)
// Fail with code and message
codeAndMsgResp := wirekit.Response.FailWithCodeAndMessage(422, "Validation failed")
printJSON("Fail with Code and Message:", codeAndMsgResp)
}
// HTTPStatusResponses demonstrates HTTP status responses.
func HTTPStatusResponses() {
fmt.Println("\n--- HTTP Status Responses ---")
// Bad Request (400)
badReqResp := wirekit.Response.BadRequest("Invalid JSON format")
printJSON("Bad Request:", badReqResp)
// Unauthorized (401)
unauthResp := wirekit.Response.Unauthorized("Token expired")
printJSON("Unauthorized:", unauthResp)
// Forbidden (403)
forbiddenResp := wirekit.Response.Forbidden("Insufficient permissions")
printJSON("Forbidden:", forbiddenResp)
// Not Found (404)
notFoundResp := wirekit.Response.NotFound("User not found")
printJSON("Not Found:", notFoundResp)
// Internal Server Error (500)
internalResp := wirekit.Response.InternalError("")
printJSON("Internal Error:", internalResp)
}
// PaginatedResponses demonstrates paginated response types.
func PaginatedResponses() {
fmt.Println("\n--- Paginated Responses ---")
users := []User{
{ID: 1, Name: "Alice", Email: "alice@example.com"},
{ID: 2, Name: "Bob", Email: "bob@example.com"},
{ID: 3, Name: "Charlie", Email: "charlie@example.com"},
}
// Using generic paginated function for type safety
paginatedResp := response.Paginated(users, 1, 10, 25)
printJSON("Paginated (Page 1):", paginatedResp)
// Page 2
page2Resp := response.Paginated(users, 2, 10, 25)
printJSON("Paginated (Page 2):", page2Resp)
// Last page
lastPageResp := response.Paginated(users, 3, 10, 25)
printJSON("Paginated (Last Page):", lastPageResp)
}
// GenericTypedResponses demonstrates type-safe generic responses.
func GenericTypedResponses() {
fmt.Println("\n--- Generic Typed Responses ---")
user := User{ID: 1, Name: "John Doe", Email: "john@example.com"}
// Type-safe success response
typedResp := response.Success(user)
printJSON("Typed Success:", typedResp)
// Access typed data
fmt.Printf(" Typed Data: ID=%d, Name=%s\n", typedResp.Data.ID, typedResp.Data.Name)
}
func main() {
wirekit.Log(wirekit.Zap).WithLevel("info").Init()
SuccessResponses()
ErrorResponses()
HTTPStatusResponses()
PaginatedResponses()
GenericTypedResponses()
wirekit.Info("response example completed")
}