Skip to content

Commit

Permalink
internal/relui: display parameters, even if their type isn't string
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
dmitshur authored and gopherbot committed Aug 10, 2022
1 parent a69abb0 commit 61de893
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
15 changes: 11 additions & 4 deletions internal/relui/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,17 @@ type homeResponse struct {
InactiveWorkflows []db.Workflow
}

func workflowParams(wf db.Workflow) map[string]string {
params := make(map[string]string)
json.Unmarshal([]byte(wf.Params.String), &params)
return params
func workflowParams(wf db.Workflow) (map[string]string, error) {
rawParams := make(map[string]json.RawMessage)
err := json.Unmarshal([]byte(wf.Params.String), &rawParams)
if err != nil {
return nil, err
}
params := make(map[string]string, len(rawParams))
for p, v := range rawParams {
params[p] = string(v)
}
return params, nil
}

// homeHandler renders the homepage.
Expand Down
14 changes: 14 additions & 0 deletions internal/relui/web_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -871,3 +871,17 @@ func TestResultDetail(t *testing.T) {
}

}

func TestWorkflowParams(t *testing.T) {
got, err := workflowParams(db.Workflow{Params: nullString(`{"greeting": "hello", "names": ["alice", "bob"]}`)})
if err != nil {
t.Fatalf("workflowParams: err = %v, wanted no error", err)
}
want := map[string]string{
"greeting": `"hello"`,
"names": `["alice", "bob"]`,
}
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("workflowParams mismatch (-want +got):\n%s", diff)
}
}

0 comments on commit 61de893

Please sign in to comment.