summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/mrexodia/wray/response.go
blob: e9815c3fb752881687e078b3f10f1fdb2381c03f (plain) (blame)
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
package wray

type Response struct {
  id string
  channel string
  successful bool
  clientId string
  supportedConnectionTypes []string
  messages []Message
  error error
}

type Message struct {
  Channel string
  Id string
  Data map[string]interface{}
}

func newResponse(data []interface{}) Response {
  headerData := data[0].(map[string]interface{})
  messagesData := data[1.:]
  messages := parseMessages(messagesData)
  var id string
  if headerData["id"] != nil {
    id = headerData["id"].(string)
  }
  supportedConnectionTypes := []string{}
  if headerData["supportedConnectionTypes"] != nil {
    d := headerData["supportedConnectionTypes"].([]interface{})
    for _, sct := range(d) {
      supportedConnectionTypes = append(supportedConnectionTypes, sct.(string))
    }
  }
  var clientId string
  if headerData["clientId"] != nil {
    clientId = headerData["clientId"].(string)
  }
  return Response{id: id,
                  clientId: clientId,
                  channel: headerData["channel"].(string),
                  successful: headerData["successful"].(bool),
                  messages: messages,
                  supportedConnectionTypes: supportedConnectionTypes}
}

func parseMessages(data []interface{}) []Message {
  messages := []Message{}
  for _, messageData := range(data) {
    m := messageData.(map[string]interface{})
    var id string
    if m["id"] != nil {
      id = m["id"].(string)
    }
    message := Message{Channel: m["channel"].(string),
                       Id: id,
                       Data: m["data"].(map[string]interface{})}
    messages = append(messages, message)
  }
  return messages
}