File: json.go

package info (click to toggle)
golang-github-containers-image 5.28.0-4
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 5,104 kB
  • sloc: sh: 194; makefile: 73
file content (90 lines) | stat: -rw-r--r-- 2,909 bytes parent folder | download | duplicates (2)
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package internal

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io"

	"github.com/containers/image/v5/internal/set"
)

// JSONFormatError is returned when JSON does not match expected format.
type JSONFormatError string

func (err JSONFormatError) Error() string {
	return string(err)
}

// ParanoidUnmarshalJSONObject unmarshals data as a JSON object, but failing on the slightest unexpected aspect
// (including duplicated keys, unrecognized keys, and non-matching types). Uses fieldResolver to
// determine the destination for a field value, which should return a pointer to the destination if valid, or nil if the key is rejected.
//
// The fieldResolver approach is useful for decoding the Policy.Transports map; using it for structs is a bit lazy,
// we could use reflection to automate this. Later?
func ParanoidUnmarshalJSONObject(data []byte, fieldResolver func(string) any) error {
	seenKeys := set.New[string]()

	dec := json.NewDecoder(bytes.NewReader(data))
	t, err := dec.Token()
	if err != nil {
		return JSONFormatError(err.Error())
	}
	if t != json.Delim('{') {
		return JSONFormatError(fmt.Sprintf("JSON object expected, got \"%s\"", t))
	}
	for {
		t, err := dec.Token()
		if err != nil {
			return JSONFormatError(err.Error())
		}
		if t == json.Delim('}') {
			break
		}

		key, ok := t.(string)
		if !ok {
			// Coverage: This should never happen, dec.Token() rejects non-string-literals in this state.
			return JSONFormatError(fmt.Sprintf("Key string literal expected, got \"%s\"", t))
		}
		if seenKeys.Contains(key) {
			return JSONFormatError(fmt.Sprintf("Duplicate key \"%s\"", key))
		}
		seenKeys.Add(key)

		valuePtr := fieldResolver(key)
		if valuePtr == nil {
			return JSONFormatError(fmt.Sprintf("Unknown key \"%s\"", key))
		}
		// This works like json.Unmarshal, in particular it allows us to implement UnmarshalJSON to implement strict parsing of the field value.
		if err := dec.Decode(valuePtr); err != nil {
			return JSONFormatError(err.Error())
		}
	}
	if _, err := dec.Token(); err != io.EOF {
		return JSONFormatError("Unexpected data after JSON object")
	}
	return nil
}

// ParanoidUnmarshalJSONObjectExactFields unmarshals data as a JSON object, but failing on the slightest unexpected aspect
// (including duplicated keys, unrecognized keys, and non-matching types). Each of the fields in exactFields
// must be present exactly once, and none other fields are accepted.
func ParanoidUnmarshalJSONObjectExactFields(data []byte, exactFields map[string]any) error {
	seenKeys := set.New[string]()
	if err := ParanoidUnmarshalJSONObject(data, func(key string) any {
		if valuePtr, ok := exactFields[key]; ok {
			seenKeys.Add(key)
			return valuePtr
		}
		return nil
	}); err != nil {
		return err
	}
	for key := range exactFields {
		if !seenKeys.Contains(key) {
			return JSONFormatError(fmt.Sprintf(`Key "%s" missing in a JSON object`, key))
		}
	}
	return nil
}