forked from mtusi2026/mtusi2026lab6
feat: add bounded observability and verified SLO incident alerts
This commit is contained in:
@@ -0,0 +1,101 @@
|
||||
// Package strictjson decodes objects with an exact set of required, non-null fields.
|
||||
package strictjson
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"slices"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
// ErrInvalid indicates invalid JSON, encoding, fields or destination field types.
|
||||
var ErrInvalid = errors.New("invalid JSON object")
|
||||
|
||||
// DecodeObject decodes one UTF-8 object with exactly the listed required fields.
|
||||
// Duplicate, unknown, missing and null fields are rejected before unmarshaling.
|
||||
// The destination must describe the supplied fields; this is not a general schema validator.
|
||||
func DecodeObject(body []byte, target any, requiredFields ...string) error {
|
||||
if !utf8.Valid(body) {
|
||||
return ErrInvalid
|
||||
}
|
||||
if err := validateObject(body, requiredFields); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := json.Unmarshal(body, target); err != nil {
|
||||
return ErrInvalid
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// validateObject enforces exact, unique, non-null required keys and one JSON value.
|
||||
// All lab1 request properties are required; this is not a generic schema validator.
|
||||
func validateObject(body []byte, requiredFields []string) error {
|
||||
return validateSchema(body, requiredFields, requiredFields)
|
||||
}
|
||||
|
||||
// validateSchema distinguishes required keys from optional allowed keys.
|
||||
func validateSchema(body []byte, requiredFields, allowedFields []string) error {
|
||||
decoder := json.NewDecoder(bytes.NewReader(body))
|
||||
token, err := decoder.Token()
|
||||
if err != nil || token != json.Delim('{') {
|
||||
return ErrInvalid
|
||||
}
|
||||
seen := make(map[string]bool, len(requiredFields))
|
||||
for decoder.More() {
|
||||
key, err := readObjectField(decoder, allowedFields, seen)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
seen[key] = true
|
||||
}
|
||||
if token, err = decoder.Token(); err != nil || token != json.Delim('}') {
|
||||
return ErrInvalid
|
||||
}
|
||||
for _, key := range requiredFields {
|
||||
if !seen[key] {
|
||||
return ErrInvalid
|
||||
}
|
||||
}
|
||||
var trailing any
|
||||
if err := decoder.Decode(&trailing); err != io.EOF {
|
||||
return ErrInvalid
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// readObjectField reads a known, unique, non-null JSON object field.
|
||||
func readObjectField(decoder *json.Decoder, requiredFields []string, seen map[string]bool) (string, error) {
|
||||
token, err := decoder.Token()
|
||||
if err != nil {
|
||||
return "", ErrInvalid
|
||||
}
|
||||
key, ok := token.(string)
|
||||
if !ok || seen[key] || !slices.Contains(requiredFields, key) {
|
||||
return "", ErrInvalid
|
||||
}
|
||||
var value json.RawMessage
|
||||
if err := decoder.Decode(&value); err != nil {
|
||||
return "", ErrInvalid
|
||||
}
|
||||
if bytes.Equal(bytes.TrimSpace(value), []byte("null")) {
|
||||
return "", ErrInvalid
|
||||
}
|
||||
return key, nil
|
||||
}
|
||||
|
||||
// DecodeOptional rejects unknown, duplicate and null fields while allowing omitted optional fields.
|
||||
func DecodeOptional(body []byte, target any, required, optional []string) error {
|
||||
if !utf8.Valid(body) {
|
||||
return ErrInvalid
|
||||
}
|
||||
allowed := append(append([]string{}, required...), optional...)
|
||||
if err := validateSchema(body, required, allowed); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := json.Unmarshal(body, target); err != nil {
|
||||
return ErrInvalid
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package strictjson
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type input struct {
|
||||
// Name contains the only accepted field in these fixed-schema tests.
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
// TestDecodeObject checks each fixed-schema rule with a fresh destination.
|
||||
func TestDecodeObject(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
body string
|
||||
want string
|
||||
wantErr error
|
||||
}{
|
||||
{"Positive: Unicode preserved", `{"name":"Алиса 👋"}`, "Алиса 👋", nil},
|
||||
{"Negative: missing field", `{}`, "", ErrInvalid},
|
||||
{"Negative: unknown field", `{"name":"alice","extra":true}`, "", ErrInvalid},
|
||||
{"Negative: duplicate field", `{"name":"alice","name":"bob"}`, "", ErrInvalid},
|
||||
{"Negative: case mismatch", `{"Name":"alice"}`, "", ErrInvalid},
|
||||
{"Negative: null field", `{"name":null}`, "", ErrInvalid},
|
||||
{"Negative: wrong value type", `{"name":12}`, "", ErrInvalid},
|
||||
{"Negative: invalid UTF-8", "{\"name\":\"\xff\"}", "", ErrInvalid},
|
||||
{"Negative: array root", `[]`, "", ErrInvalid},
|
||||
{"Negative: unclosed object", `{"name":"alice"`, "", ErrInvalid},
|
||||
{"Negative: invalid value", `{"name":}`, "", ErrInvalid},
|
||||
{"Negative: invalid key", `{name:"alice"}`, "", ErrInvalid},
|
||||
{"Negative: trailing value", `{"name":"alice"} {}`, "", ErrInvalid},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
// Arrange.
|
||||
var destination input
|
||||
|
||||
// Act.
|
||||
err := DecodeObject([]byte(tc.body), &destination, "name")
|
||||
|
||||
// Assert.
|
||||
if !errors.Is(err, tc.wantErr) {
|
||||
t.Fatalf("error=%v, want=%v", err, tc.wantErr)
|
||||
}
|
||||
if destination.Name != tc.want {
|
||||
t.Fatalf("decoded name=%q, want=%q", destination.Name, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package strictjson
|
||||
|
||||
import "testing"
|
||||
|
||||
// TestDecodeOptional distinguishes omission from explicit null and duplicate fields.
|
||||
func TestDecodeOptional(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
body string
|
||||
valid bool
|
||||
}{
|
||||
{"omitted", `{"text":"ok"}`, true},
|
||||
{"provided", `{"text":"ok","attachment_ids":["a"]}`, true},
|
||||
{"null", `{"text":"ok","attachment_ids":null}`, false},
|
||||
{"unknown", `{"text":"ok","sender_id":"a"}`, false},
|
||||
{"duplicate", `{"text":"ok","text":"other"}`, false},
|
||||
{"missing required", `{"attachment_ids":[]}`, false},
|
||||
{"wrong type", `{"text":"ok","attachment_ids":12}`, false},
|
||||
{"invalid utf8", string([]byte{255}), false},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
var target struct {
|
||||
Text string `json:"text"`
|
||||
IDs []string `json:"attachment_ids"`
|
||||
}
|
||||
err := DecodeOptional([]byte(tc.body), &target, []string{"text"}, []string{"attachment_ids"})
|
||||
if (err == nil) != tc.valid {
|
||||
t.Fatalf("validation: %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user