Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add descriptions for services, methods, and fields #166

Merged
merged 3 commits into from Feb 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/grpcui/grpcui.go
Expand Up @@ -6,6 +6,7 @@ package main

import (
"bytes"
"context"
"encoding/hex"
"flag"
"fmt"
Expand All @@ -29,7 +30,6 @@ import (
"github.com/jhump/protoreflect/grpcreflect"
"github.com/pkg/browser"
"golang.org/x/crypto/ssh/terminal"
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
insecurecreds "google.golang.org/grpc/credentials/insecure"
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -5,7 +5,7 @@ go 1.15
require (
github.com/fullstorydev/grpcurl v1.8.6
github.com/golang/protobuf v1.5.2
github.com/jhump/protoreflect v1.10.4-0.20220210001327-6224817bacc5
github.com/jhump/protoreflect v1.11.0
github.com/pkg/browser v0.0.0-20180916011732-0a3d74bf9ce4
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9
golang.org/x/net v0.0.0-20200822124328-c89045814202
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Expand Up @@ -110,8 +110,8 @@ github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/jhump/protoreflect v1.10.3/go.mod h1:7GcYQDdMU/O/BBrl/cX6PNHpXh6cenjd8pneu5yW7Tg=
github.com/jhump/protoreflect v1.10.4-0.20220210001327-6224817bacc5 h1:KIpt99Cgc9Vv3Hjbx/ke8ZQAzcH7AzM7/HxFCumGI7s=
github.com/jhump/protoreflect v1.10.4-0.20220210001327-6224817bacc5/go.mod h1:U7aMIjN0NWq9swDP7xDdoMfRHb35uiuTd3Z9nFXJf5E=
github.com/jhump/protoreflect v1.11.0 h1:bvACHUD1Ua/3VxY4aAMpItKMhhwbimlKFJKsLsVgDjU=
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This version includes a fix for printing methods: without it, comments on option statements were never included.

github.com/jhump/protoreflect v1.11.0/go.mod h1:U7aMIjN0NWq9swDP7xDdoMfRHb35uiuTd3Z9nFXJf5E=
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
Expand Down
16 changes: 16 additions & 0 deletions handlers.go
Expand Up @@ -192,6 +192,7 @@ type fieldDef struct {
IsMap bool `json:"isMap"`
IsRequired bool `json:"isRequired"`
DefaultVal interface{} `json:"defaultVal"`
Description string `json:"description"`
}

type enumValDef struct {
Expand Down Expand Up @@ -349,6 +350,21 @@ func (s *schema) processField(fd *desc.FieldDescriptor) fieldDef {
def.Type = typeMap[fd.GetType()]
}

desc, err := protoPrinter.PrintProtoToString(fd)
if err != nil {
// generate simple description with no comments or options
var label string
if fd.IsRequired() {
label = "required "
} else if fd.IsRepeated() {
label = "repeated "
} else if fd.IsProto3Optional() || !fd.GetFile().IsProto3() {
label = "optional "
}
desc = fmt.Sprintf("%s%s %s = %d;", label, def.Type, fd.GetName(), fd.GetNumber())
}
def.Description = desc

return def
}

Expand Down
6 changes: 3 additions & 3 deletions internal/resources/webform/bindata.go

Large diffs are not rendered by default.

76 changes: 63 additions & 13 deletions internal/resources/webform/webform-sample.css
Expand Up @@ -4,21 +4,67 @@
}

.grpc-desc {
margin-bottom: 8px;
margin-left: 16px;
margin-bottom: 0;
}

.grpc-desc span.grpc-form-label {
font-size: 125%;
width: 130px;
float: left;
line-height: 30px;
.grpc-desc td {
vertical-align: top;
}

#grpc-desc-selects td {
padding-top: 12px;
}

.grpc-desc select {
height: 30px;
}

.grpc-desc pre {
margin: 0 0 0 36px;
padding-left: 20px;
border-left: 1px solid #d8d8d8;
color: #777;
}

.grpc-desc button#grpc-descriptions-toggle {
position: absolute;
z-index: 3;
padding: 0;
margin-top: 10px;
margin-left: 10px;
border: none;
background: white;
font-size: 150%;
}

.grpc-desc div.grpc-form-label {
font-size: 110%;
width: 128px;
line-height: 30px;
text-align: right;
margin-right: 12px;
}

#grpc-descriptions {
width: 100%;
overflow: scroll;
}

#grpc-service-description {
padding-top:14px;
}

#grpc-method-description {
font-weight: bold;
color: black;
padding-top: 12px;
padding-bottom: 12px;
}

#grpc-service-description-end {
padding-bottom:14px;
}

#grpc-request-response {
margin-top: 24px;
border: none;
Expand All @@ -34,8 +80,7 @@

#grpc-request-response li.ui-tabs-tab {
border: 1px solid #ccc;
margin: 0;
margin-right: -1px;
margin: 0 -1px 0 0;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This and the one below were cleanup suggested by my IDE.

padding: 4px 8px;
border-top-right-radius: 6px;
border-top-left-radius: 6px;
Expand Down Expand Up @@ -118,8 +163,7 @@ textarea#grpc-request-raw-text {
}

table#grpc-request-metadata-form {
margin: 0;
margin-left: 4px;
margin: 0 0 0 4px;
border-collapse: collapse;
}

Expand Down Expand Up @@ -174,7 +218,7 @@ table#grpc-request-metadata-form {

#grpc-request-form td {
padding: 16px 0 0 16px;
font-size: 13.2px;
font-size: 13px;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto, this and the change at the bottom were cleanup suggested by the IDE (due to not all browsers supporting fractional pixels).

}

#grpc-request-form td:first-of-type {
Expand Down Expand Up @@ -254,6 +298,12 @@ table#grpc-request-metadata-form {
font-size: 120%;
}

.ui-tooltip.grpc-field-description {
white-space: pre;
font-family: "Courier New", Courier, monospace;
max-width: none;
}

#grpc-request-form td.toggle_presence {
width: 1px;
}
Expand Down Expand Up @@ -442,7 +492,7 @@ textarea.grpc-response-textarea {

#grpc-response-data td {
padding: 12px 0 0 12px;
font-size: 13.2px;
font-size: 13px;
font-family: "courier new", courier, monospace;
}

Expand Down
63 changes: 49 additions & 14 deletions internal/resources/webform/webform-template.html
@@ -1,17 +1,42 @@
<div id="grpc-form">
<div class="grpc-desc" id="grpc-service-select"><span class="grpc-form-label">Service name:</span>
<select name="grpc-service" id="grpc-service">
{{ range .Services }}
<option value="{{.}}">{{.}}</option>
{{ end }}
</select>
</div>

<div class="grpc-desc" id="grpc-method-select"><span class="grpc-form-label">Method name:</span>
<select name="grpc-method" id="grpc-method">
<!-- populated by JavaScript when grpc-service is selected -->
</select>
</div>
<table class="grpc-desc">
<tr>
<td>
<table id="grpc-desc-selects">
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hate adding a nested table. But I played around with the layout quite a lot, and I could not figure out how to preserve the existing look without doing this. I tried floating divs and simpler tables with more divs inside and fewer table cells and no nesting. But I just couldn't figure out how to get the look and layout behavior I was aiming for without this.

<!-- service -->
<tr>
<td>
<div class="grpc-form-label">Service name:</div>
</td>
<td id="grpc-service-select">
<select name="grpc-service" id="grpc-service">
{{ range .Services }}
<option value="{{.}}">{{.}}</option>
{{ end }}
</select>
</td>
</tr>
<!-- method -->
<tr>
<td>
<div class="grpc-form-label">Method name:</div>
</td>
<td id="grpc-method-select">
<select name="grpc-method" id="grpc-method">
<!-- populated by JavaScript when grpc-service is selected -->
</select>
</td>
</tr>
</table>
</td>
<td id="grpc-descriptions">
<button id="grpc-descriptions-toggle">»</button>
<pre id="grpc-service-description"></pre>
<pre id="grpc-method-description"></pre>
<pre id="grpc-service-description-end"></pre>
</td>
</tr>
</table>

<div id="grpc-request-response">
<ul>
Expand Down Expand Up @@ -85,12 +110,22 @@ <h3>Response Trailers</h3>
],
{{- end}}
};
var svcDescs = {
{{- range $name, $desc := .SvcDescs}}
"{{$name}}": "{{$desc}}",
{{- end}}
};
var mtdDescs = {
{{- range $name, $desc := .MtdDescs}}
"{{$name}}": "{{$desc}}",
{{- end}}
};
var headers = [
{{- range .DefaultMetadata}}
{"name": "{{.Name}}", "value": "{{.Value}}"},
{{- end}}
];

initGRPCForm(services, '{{ .InvokeURI }}', '{{ .MetadataURI }}', {{ .Debug }}, headers);
initGRPCForm(services, svcDescs, mtdDescs, '{{ .InvokeURI }}', '{{ .MetadataURI }}', {{ .Debug }}, headers);
})();
</script>