/
registry.go
140 lines (129 loc) · 3.36 KB
/
registry.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
//
// This source file is part of the carousell/aggproto open source project
//
// Copyright © 2021 Carousell and the project authors
// Licensed under Apache License v2.0
//
// See https://github.com/carousell/aggproto/blob/master/LICENSE for license information
// See https://github.com/carousell/aggproto/graphs/contributors for the list of project authors
//
package app
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"github.com/carousell/aggproto/app/components"
registry2 "github.com/carousell/aggproto/pkg/registry"
"github.com/carousell/aggproto/app/models"
"github.com/maxence-charriere/go-app/v8/pkg/app"
)
type registry struct {
app.Compo
MessageList []*models.Message
searchTerm string
SelectedMessages []*models.Message
}
func (r *registry) OnMount(ctx app.Context) {
ctx.Async(func() {
msgs := r.loadMessages("*")
if len(msgs) == 0 {
return
}
ctx.Dispatch(func() {
r.MessageList = msgs
r.Update()
})
})
}
func (r *registry) Render() app.UI {
return app.Div().Class("registry").
Body(
app.Stack().Vertical().Content(
app.Stack().Content(
app.Text("Search Message:"),
app.DataList().ID("messages").
Body(
app.Range(r.MessageList).Slice(func(i int) app.UI {
msg := r.MessageList[i]
return app.Option().Value(fmt.Sprintf("%s.%s", msg.PackageName, msg.Name))
})),
app.Input().AutoComplete(true).List("messages").OnInput(func(ctx app.Context, e app.Event) {
r.searchTerm = ctx.JSSrc.Get("value").String()
}),
app.Button().Text("search").OnClick(func(ctx app.Context, e app.Event) {
ctx.Async(func() {
r.loadMessage(ctx)
})
}),
),
app.Range(r.SelectedMessages).Slice(func(i int) app.UI {
msg := r.SelectedMessages[i]
return &messageCard{msg: msg, fieldName: msg.Name, repeated: false}
}),
),
)
}
func (r *registry) loadMessage(ctx app.Context) {
msgs := r.loadMessages(r.searchTerm)
if len(msgs) == 0 {
return
}
ctx.Dispatch(func() {
r.SelectedMessages = msgs
r.Update()
})
}
func (r *registry) loadMessages(prefix string) []*models.Message {
var msgs []*models.Message
resp, err := http.Get(fmt.Sprintf("/api/registry/search/message/%s", prefix))
if err != nil {
app.Log(err)
return nil
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
app.Log(err)
return nil
}
defer resp.Body.Close()
err = json.Unmarshal(body, &msgs)
if err != nil {
app.Log(err)
return nil
}
return msgs
}
type messageCard struct {
app.Compo
msg *models.Message
fieldName string
repeated bool
}
func (mc *messageCard) Render() app.UI {
var fieldNames []app.UI
for _, field := range mc.msg.Fields {
if field.Type == registry2.FieldTypeMessage.GoTypeString() {
fieldNames = append(fieldNames, &messageCard{msg: field.Message, fieldName: field.Name, repeated: field.Repeated})
} else {
fieldNames = append(fieldNames,
app.Div().Class("row").Body(
app.P().Text(field.Name),
app.If(field.Repeated, app.P().Text("[]")),
app.P().Text(field.Type),
),
)
}
}
return app.Div().Class("message-card").Body(
components.CollapsibleList().
WithTitle(
app.Div().Class("row").Body(
app.P().Text(mc.fieldName),
app.If(mc.repeated, app.P().Text("[]")),
app.P().Text(fmt.Sprintf("%s.%s", mc.msg.PackageName, mc.msg.Name)),
),
).
WithElements(fieldNames),
)
}