mirror of
https://github.com/fleetdm/fleet
synced 2026-04-21 13:37:30 +00:00
Implements patch policies #31914 - https://github.com/fleetdm/fleet/pull/40816 - https://github.com/fleetdm/fleet/pull/41248 - https://github.com/fleetdm/fleet/pull/41276 - https://github.com/fleetdm/fleet/pull/40948 - https://github.com/fleetdm/fleet/pull/40837 - https://github.com/fleetdm/fleet/pull/40956 - https://github.com/fleetdm/fleet/pull/41168 - https://github.com/fleetdm/fleet/pull/41171 - https://github.com/fleetdm/fleet/pull/40691 - https://github.com/fleetdm/fleet/pull/41524 - https://github.com/fleetdm/fleet/pull/41674 --------- Co-authored-by: Jonathan Katz <44128041+jkatz01@users.noreply.github.com> Co-authored-by: jkatz01 <yehonatankatz@gmail.com> Co-authored-by: RachelElysia <71795832+RachelElysia@users.noreply.github.com> Co-authored-by: Jahziel Villasana-Espinoza <jahziel@fleetdm.com>
54 lines
1.2 KiB
Go
54 lines
1.2 KiB
Go
package optjson
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
)
|
|
|
|
// StringOr is a JSON value that can be a string or a different type of object
|
|
// (e.g. somewhat common for a string or an array of strings, but can also be
|
|
// a string or an object, etc.).
|
|
type StringOr[T any] struct {
|
|
String string
|
|
Other T
|
|
IsOther bool
|
|
}
|
|
|
|
func (s StringOr[T]) MarshalJSON() ([]byte, error) {
|
|
if s.IsOther {
|
|
return json.Marshal(s.Other)
|
|
}
|
|
return json.Marshal(s.String)
|
|
}
|
|
|
|
func (s *StringOr[T]) UnmarshalJSON(data []byte) error {
|
|
if bytes.HasPrefix(data, []byte(`"`)) {
|
|
s.IsOther = false
|
|
return json.Unmarshal(data, &s.String)
|
|
}
|
|
s.IsOther = true
|
|
return json.Unmarshal(data, &s.Other)
|
|
}
|
|
|
|
// BoolOr is a JSON value that can be a boolean or a different type of object
|
|
type BoolOr[T any] struct {
|
|
Bool bool
|
|
Other T
|
|
IsOther bool
|
|
}
|
|
|
|
func (s BoolOr[T]) MarshalJSON() ([]byte, error) {
|
|
if s.IsOther {
|
|
return json.Marshal(s.Other)
|
|
}
|
|
return json.Marshal(s.Bool)
|
|
}
|
|
|
|
func (s *BoolOr[T]) UnmarshalJSON(data []byte) error {
|
|
if bytes.Equal(data, []byte(`true`)) || bytes.Equal(data, []byte(`false`)) {
|
|
s.IsOther = false
|
|
return json.Unmarshal(data, &s.Bool)
|
|
}
|
|
s.IsOther = true
|
|
return json.Unmarshal(data, &s.Other)
|
|
}
|