-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson_wire.go
More file actions
32 lines (28 loc) · 763 Bytes
/
Copy pathjson_wire.go
File metadata and controls
32 lines (28 loc) · 763 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package yin
import (
"encoding/json"
"errors"
"fmt"
)
func validateJSONFormatVersion(raw json.RawMessage, present bool, want int) error {
if !present {
return errors.New("missing formatVersion")
}
var got *int
if err := json.Unmarshal(raw, &got); err != nil {
return fmt.Errorf("formatVersion must be an integer: %w", err)
}
if got == nil {
return errors.New("formatVersion must be an integer")
}
if *got != want {
return fmt.Errorf("unsupported formatVersion %d, want %d", *got, want)
}
return nil
}
func requireJSONFormatVersion(raw json.RawMessage, want int, boundary string) error {
if err := validateJSONFormatVersion(raw, raw != nil, want); err != nil {
return fmt.Errorf("yin: malformed %s: %w", boundary, err)
}
return nil
}