Debian Codegraph @

signature/internal/json.go

raw
1 package internal
2
3 import (
4 "bytes"
5 "encoding/json"
6 "fmt"
7 "io"
8
9 "go.podman.io/image/v5/internal/set"
10 )
11
12 // JSONFormatError is returned when JSON does not match expected format.
13 type JSONFormatError string
14
15 func (err JSONFormatError) Error() string {
16 return string(err)
17 }
18
19 // ParanoidUnmarshalJSONObject unmarshals data as a JSON object, but failing on the slightest unexpected aspect
20 // (including duplicated keys, unrecognized keys, and non-matching types). Uses fieldResolver to
21 // determine the destination for a field value, which should return a pointer to the destination if valid, or nil if the key is rejected.
22 //
23 // The fieldResolver approach is useful for decoding the Policy.Transports map; using it for structs is a bit lazy,
24 // we could use reflection to automate this. Later?
25 func ParanoidUnmarshalJSONObject(data []byte, fieldResolver func(string) any) error {
26 seenKeys := set.New[string]()
27
28 dec := json.NewDecoder(bytes.NewReader(data))
29 t, err := dec.Token()
30 if err != nil {
31 return JSONFormatError(err.Error())
32 }
33 if t != json.Delim('{') {
34 return JSONFormatError(fmt.Sprintf("JSON object expected, got %#v", t))
35 }
36 for {
37 t, err := dec.Token()
38 if err != nil {
39 return JSONFormatError(err.Error())
40 }
41 if t == json.Delim('}') {
42 break
43 }
44
45 key, ok := t.(string)
46 if !ok {
47 // Coverage: This should never happen, dec.Token() rejects non-string-literals in this state.
48 return JSONFormatError(fmt.Sprintf("Key string literal expected, got %#v", t))
49 }
50 if seenKeys.Contains(key) {
51 return JSONFormatError(fmt.Sprintf("Duplicate key %q", key))
52 }
53 seenKeys.Add(key)
54
55 valuePtr := fieldResolver(key)
56 if valuePtr == nil {
57 return JSONFormatError(fmt.Sprintf("Unknown key %q", key))
58 }
59 // This works like json.Unmarshal, in particular it allows us to implement UnmarshalJSON to implement strict parsing of the field value.
60 if err := dec.Decode(valuePtr); err != nil {
61 return JSONFormatError(err.Error())
62 }
63 }
64 if _, err := dec.Token(); err != io.EOF {
65 return JSONFormatError("Unexpected data after JSON object")
66 }
67 return nil
68 }
69
70 // ParanoidUnmarshalJSONObjectExactFields unmarshals data as a JSON object, but failing on the slightest unexpected aspect
71 // (including duplicated keys, unrecognized keys, and non-matching types). Each of the fields in exactFields
72 // must be present exactly once, and none other fields are accepted.
73 func ParanoidUnmarshalJSONObjectExactFields(data []byte, exactFields map[string]any) error {
74 seenKeys := set.New[string]()
75 if err := ParanoidUnmarshalJSONObject(data, func(key string) any {
76 if valuePtr, ok := exactFields[key]; ok {
77 seenKeys.Add(key)
78 return valuePtr
79 }
80 return nil
81 }); err != nil {
82 return err
83 }
84 for key := range exactFields {
85 if !seenKeys.Contains(key) {
86 return JSONFormatError(fmt.Sprintf(`Key %q missing in a JSON object`, key))
87 }
88 }
89 return nil
90 }