40 lines
1.0 KiB
Go
40 lines
1.0 KiB
Go
// Copyright (c) 2026 Storm Dragon
|
|
// Authors: Storm Dragon, Angus Kola
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
package interaction
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"errors"
|
|
"strings"
|
|
)
|
|
|
|
// UnmarshalJSON keeps a malformed secret flag from silently becoming an
|
|
// ordinary entry (encoding/json otherwise accepts null for a boolean). The
|
|
// wire decoder separately rejects duplicate keys and bounds nesting/framing.
|
|
func (element *Element) UnmarshalJSON(data []byte) error {
|
|
type plain Element
|
|
var value plain
|
|
decoder := json.NewDecoder(bytes.NewReader(data))
|
|
decoder.DisallowUnknownFields()
|
|
if err := decoder.Decode(&value); err != nil {
|
|
return err
|
|
}
|
|
var fields map[string]json.RawMessage
|
|
if err := json.Unmarshal(data, &fields); err != nil {
|
|
return err
|
|
}
|
|
for name, raw := range fields {
|
|
if strings.EqualFold(name, "secret") {
|
|
flag := string(bytes.TrimSpace(raw))
|
|
if name != "secret" || flag != "true" && flag != "false" {
|
|
return errors.New("secret must be a boolean")
|
|
}
|
|
}
|
|
}
|
|
*element = Element(value)
|
|
return nil
|
|
}
|