-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.go
153 lines (127 loc) · 3.31 KB
/
api.go
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
143
144
145
146
147
148
149
150
151
152
153
package main
import (
"context"
"fmt"
"net/http"
"sync"
)
// ApiError используется в коде, который получается в результате генерации
// считаем что это какая-то общеизвестная структура
type ApiError struct {
HTTPStatus int
Err error
}
func (ae ApiError) Error() string {
return ae.Err.Error()
}
// ----------------
const (
statusUser = 0
statusModerator = 10
statusAdmin = 20
)
type MyApi struct {
statuses map[string]int
users map[string]*User
nextID uint64
mu *sync.RWMutex
}
func NewMyApi() *MyApi {
return &MyApi{
statuses: map[string]int{
"user": 0,
"moderator": 10,
"admin": 20,
},
users: map[string]*User{
"rvasily": &User{
ID: 42,
Login: "rvasily",
FullName: "Vasily Romanov",
Status: statusAdmin,
},
},
nextID: 43,
mu: &sync.RWMutex{},
}
}
type ProfileParams struct {
Login string `apivalidator:"required"`
}
type CreateParams struct {
Login string `apivalidator:"required,min=10"`
Name string `apivalidator:"paramname=full_name"`
Status string `apivalidator:"enum=user|moderator|admin,default=user"`
Age int `apivalidator:"min=0,max=128"`
}
type User struct {
ID uint64 `json:"id"`
Login string `json:"login"`
FullName string `json:"full_name"`
Status int `json:"status"`
}
type NewUser struct {
ID uint64 `json:"id"`
}
// apigen:api {"url": "/user/profile", "auth": false}
func (srv *MyApi) Profile(ctx context.Context, in ProfileParams) (*User, error) {
if in.Login == "bad_user" {
return nil, fmt.Errorf("bad user")
}
srv.mu.RLock()
user, exist := srv.users[in.Login]
srv.mu.RUnlock()
if !exist {
return nil, ApiError{http.StatusNotFound, fmt.Errorf("user not exist")}
}
return user, nil
}
// apigen:api {"url": "/user/create", "auth": true, "method": "POST"}
func (srv *MyApi) Create(ctx context.Context, in CreateParams) (*NewUser, error) {
if in.Login == "bad_username" {
return nil, fmt.Errorf("bad user")
}
srv.mu.Lock()
defer srv.mu.Unlock()
_, exist := srv.users[in.Login]
if exist {
return nil, ApiError{http.StatusConflict, fmt.Errorf("user %s exist", in.Login)}
}
id := srv.nextID
srv.nextID++
srv.users[in.Login] = &User{
ID: id,
Login: in.Login,
FullName: in.Name,
Status: srv.statuses[in.Status],
}
return &NewUser{id}, nil
}
// 2-я часть
// это похожая структура, с теми же методами, но у них другие параметры
type OtherApi struct {
}
func NewOtherApi() *OtherApi {
return &OtherApi{}
}
type OtherCreateParams struct {
Username string `apivalidator:"required,min=3"`
Name string `apivalidator:"paramname=account_name"`
Class string `apivalidator:"enum=warrior|sorcerer|rouge,default=warrior"`
Level int `apivalidator:"min=1,max=50"`
}
type OtherUser struct {
ID uint64 `json:"id"`
Login string `json:"login"`
FullName string `json:"full_name"`
Level int `json:"level"`
}
// apigen:api {"url": "/user/create", "auth": true, "method": "POST"}
func (srv *OtherApi) Create(ctx context.Context, in OtherCreateParams) (*OtherUser, error) {
return &OtherUser{
ID: 12,
Login: in.Username,
FullName: in.Name,
Level: in.Level,
}, nil
}