-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmsg_types.go
134 lines (117 loc) · 3.8 KB
/
msg_types.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
package main
import (
"io/ioutil"
"regexp"
"strings"
"time"
"github.com/gorilla/websocket"
"github.com/tkrajina/typescriptify-golang-structs/typescriptify"
)
func convertToTS() {
converter := typescriptify.New().
Add(Client{}).
Add(Session{}).
Add(ClientConnectMsg{}).
Add(CreateSessionMsg{}).
Add(UpdateClientMsg{}).
Add(AddClientToSessionMsg{}).
Add(ClientJoinedSessionMsg{}).
Add(ClientLeftSessionMsg{}).
Add(BroadcastToSessionMsg{}).
Add(BroadcastFromSessionMsg{}).
Add(ErrorMsg{}).
Add(InfoMsg{})
converter.CreateInterface = true
converter.ManageType(time.Time{}, typescriptify.TypeOptions{
TSType: "string",
})
converter.BackupDir = ""
tsString, err := converter.Convert(make(map[string]string))
if err != nil {
panic(err.Error())
}
regex, _ := regexp.Compile("([A-z]+)Msg {\n type: string;")
msgTypeNames := make([]string, 1)
modifiedTs := regex.ReplaceAllStringFunc(tsString, func(str string) string {
msgTypeName := regex.FindStringSubmatch(str)[1]
msgTypeNames = append(msgTypeNames, msgTypeName+"Msg")
return strings.Replace(str, "type: string;", "type: \""+msgTypeName+"\";", -1)
})
modifiedTs = strings.ReplaceAll(modifiedTs, "\n", "\n ")
unionTypeStr := " export type Msg = " + msgTypeNames[0] + strings.Join(msgTypeNames[1:], " | ")
modifiedTs = "export namespace ServerTypes {\n" + unionTypeStr + "\n" + modifiedTs + "\n}"
ioutil.WriteFile("./models.ts", []byte(modifiedTs), 0644)
}
// Client - Connected client
type Client struct {
ID string `json:"id"`
Name string `json:"name"`
conn *websocket.Conn
activeSessionID string
LastJoinTime time.Time `json:"lastJoinTime"`
}
// Session - Session for sharing content
type Session struct {
ID string `json:"id"`
OwnerID string `json:"ownerId"`
ClientIDs []string `json:"clientIds"`
createdDate time.Time
}
// CreateSessionMsg - Sent from client to create session
type CreateSessionMsg struct {
Type string `json:"type"`
}
// ClientConnectMsg - Sent to client on connecting
type ClientConnectMsg struct {
Type string `json:"type"`
Client Client `json:"client"`
}
// UpdateClientMsg - Updates a client
type UpdateClientMsg struct {
Type string `json:"type"`
Name string `json:"name"`
}
// AddClientToSessionMsg - Websocket message
type AddClientToSessionMsg struct {
Type string `json:"type"`
SessionID string `json:"sessionId"`
AddClientID string `json:"addClientId"`
}
// ClientJoinedSessionMsg -
type ClientJoinedSessionMsg struct {
Type string `json:"type"`
ClientID string `json:"clientId"`
SessionID string `json:"sessionId"`
SessionOwnerID string `json:"sessionOwnerId"`
ClientMap map[string]Client `json:"clientMap"`
}
// ClientLeftSessionMsg -
type ClientLeftSessionMsg struct {
Type string `json:"type"`
ClientID string `json:"clientId"`
SessionID string `json:"sessionId"`
SessionOwnerID string `json:"sessionOwnerId"`
ClientMap map[string]Client `json:"clientMap"`
}
// BroadcastToSessionMsg - Used by client to send content to all clients in session
type BroadcastToSessionMsg struct {
Type string `json:"type"`
Payload string `json:"payload"`
}
// BroadcastFromSessionMsg - Used by server to send content all clients in a session
type BroadcastFromSessionMsg struct {
Type string `json:"type"`
FromSessionOwner bool `json:"fromSessionOwner"`
SenderID string `json:"senderId"`
Payload string `json:"payload"`
}
// ErrorMsg - Websocket error message
type ErrorMsg struct {
Type string `json:"type"`
Message string `json:"message"`
}
// InfoMsg - Websocket info message
type InfoMsg struct {
Type string `json:"type"`
Message string `json:"message"`
}