Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 19 additions & 47 deletions alert_rules.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 54 additions & 0 deletions alert_rules_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package flashduty

import (
"context"
"encoding/json"
"net/http"
"testing"
)

func TestAlertRuleUpdateV2PreservesInvestigationTargetsPresence(t *testing.T) {
tests := []struct {
name string
targets []InvestigationTarget
want string
}{
{name: "omit keeps existing targets"},
{name: "empty clears targets", targets: []InvestigationTarget{}, want: `[]`},
{
name: "replace targets",
targets: []InvestigationTarget{{
Kind: "dashboard",
Dashboard: DashboardInvestigationTarget{
DashboardID: "01900000-0000-7000-8000-000000000001",
},
}},
want: `[{"dashboard":{"dashboard_id":"01900000-0000-7000-8000-000000000001"},"kind":"dashboard"}]`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
client := newTestClient(t, func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost || r.URL.Path != "/monit/rule/v2/update" {
t.Errorf("unexpected request: %s %s", r.Method, r.URL.Path)
}
var body map[string]json.RawMessage
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
t.Errorf("decode request: %v", err)
}
if got := string(body["investigation_targets"]); got != tt.want {
t.Errorf("investigation_targets = %q, want %q", got, tt.want)
}
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(map[string]any{"data": body})
})
_, _, err := client.AlertRules.WriteUpdateV2(context.Background(), &AlertRuleV2{
ID: 123,
InvestigationTargets: tt.targets,
})
if err != nil {
t.Fatal(err)
}
})
}
}
70 changes: 70 additions & 0 deletions applications.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions internal/cmd/gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -853,6 +853,11 @@ func (g *Gen) emitStruct(name string, s map[string]any) string {
case inReq && needsPointer && (isNullable(pv) || preserveAbsence):
jsonTag = k + ",omitempty"
toonTag = k + ",omitempty"
case inReq && !required[k] && preserveAbsence && strings.HasPrefix(gt, "[]"):
// Preserve nil (omitted) versus an explicit empty slice (clear).
// omitempty would drop both and silently keep the stored value.
jsonTag = k + ",omitzero"
toonTag = k + ",omitempty"
case inReq && !required[k]:
toonTag = k + ",omitempty"
if isStructField {
Expand Down
Loading