forked from aertje/cloud-tasks-emulator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
protohelpers.go
68 lines (63 loc) · 1.63 KB
/
protohelpers.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
package main
import (
"net/http"
tasks "google.golang.org/genproto/googleapis/cloud/tasks/v2beta3"
rpccode "google.golang.org/genproto/googleapis/rpc/code"
)
func toHTTPMethod(taskMethod tasks.HttpMethod) string {
switch taskMethod {
case tasks.HttpMethod_GET:
return http.MethodGet
case tasks.HttpMethod_POST:
return http.MethodPost
case tasks.HttpMethod_DELETE:
return http.MethodDelete
case tasks.HttpMethod_HEAD:
return http.MethodHead
case tasks.HttpMethod_OPTIONS:
return http.MethodOptions
case tasks.HttpMethod_PATCH:
return http.MethodPatch
case tasks.HttpMethod_PUT:
return http.MethodPut
default:
panic("Unsupported http method")
}
}
func toRPCStatusCode(statusCode int) int32 {
switch statusCode {
case 200:
return int32(rpccode.Code_OK)
case 400:
// TODO: or rpccode.Code_FAILED_PRECONDITION
// TODO: or rpcCode.Code_OUT_OF_RANGE
return int32(rpccode.Code_INVALID_ARGUMENT)
case 401:
return int32(rpccode.Code_UNAUTHENTICATED)
case 403:
return int32(rpccode.Code_PERMISSION_DENIED)
case 404:
return int32(rpccode.Code_NOT_FOUND)
case 409:
// TODO: or rpccde.Code_ABORTED
return int32(rpccode.Code_ALREADY_EXISTS)
case 429:
return int32(rpccode.Code_RESOURCE_EXHAUSTED)
case 499:
return int32(rpccode.Code_CANCELLED)
case 500:
//TODO: or rpccode.Code_DATA_LOSS
return int32(rpccode.Code_INTERNAL)
case 501:
return int32(rpccode.Code_UNIMPLEMENTED)
case 503:
return int32(rpccode.Code_UNAVAILABLE)
case 504:
return int32(rpccode.Code_DEADLINE_EXCEEDED)
default:
return int32(rpccode.Code_UNKNOWN)
}
}
func toCodeName(rpcCode int32) string {
return rpccode.Code_name[rpcCode]
}