Skip to content

Commit 61de893

Browse files
dmitshurgopherbot
authored andcommitted
internal/relui: display parameters, even if their type isn't string
Some parameters have a type other than string, so unmarshaling them into a map with string value type would fail. Prefer to make such problems visible by returning any non-nil errors from workflowParams to caller. For now, display parameter values in their JSON form, since it's simple, quite human readable, and has a nice side-effect of turning any leading or trailing spaces apparent. (In the future we can consider investing into a more elaborate parameter layout, as needed.) Fixes golang/go#54240. Change-Id: I2a6048e5b5b0d42257b6088c8ba01b26216a3c39 Reviewed-on: https://go-review.googlesource.com/c/build/+/422554 Reviewed-by: Jenny Rakoczy <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]> Run-TryBot: Dmitri Shuralyov <[email protected]> Auto-Submit: Dmitri Shuralyov <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent a69abb0 commit 61de893

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

internal/relui/web.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,17 @@ type homeResponse struct {
144144
InactiveWorkflows []db.Workflow
145145
}
146146

147-
func workflowParams(wf db.Workflow) map[string]string {
148-
params := make(map[string]string)
149-
json.Unmarshal([]byte(wf.Params.String), &params)
150-
return params
147+
func workflowParams(wf db.Workflow) (map[string]string, error) {
148+
rawParams := make(map[string]json.RawMessage)
149+
err := json.Unmarshal([]byte(wf.Params.String), &rawParams)
150+
if err != nil {
151+
return nil, err
152+
}
153+
params := make(map[string]string, len(rawParams))
154+
for p, v := range rawParams {
155+
params[p] = string(v)
156+
}
157+
return params, nil
151158
}
152159

153160
// homeHandler renders the homepage.

internal/relui/web_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -871,3 +871,17 @@ func TestResultDetail(t *testing.T) {
871871
}
872872

873873
}
874+
875+
func TestWorkflowParams(t *testing.T) {
876+
got, err := workflowParams(db.Workflow{Params: nullString(`{"greeting": "hello", "names": ["alice", "bob"]}`)})
877+
if err != nil {
878+
t.Fatalf("workflowParams: err = %v, wanted no error", err)
879+
}
880+
want := map[string]string{
881+
"greeting": `"hello"`,
882+
"names": `["alice", "bob"]`,
883+
}
884+
if diff := cmp.Diff(want, got); diff != "" {
885+
t.Errorf("workflowParams mismatch (-want +got):\n%s", diff)
886+
}
887+
}

0 commit comments

Comments
 (0)