Skip to content

Commit 7ba0400

Browse files
authored
add custom fields to all top level roles (#162)
* add custom fields to all top level roles * add tests * update Key => PublicKey
1 parent d74a4b4 commit 7ba0400

File tree

2 files changed

+66
-16
lines changed

2 files changed

+66
-16
lines changed

data/types.go

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ type Root struct {
9999
Expires time.Time `json:"expires"`
100100
Keys map[string]*PublicKey `json:"keys"`
101101
Roles map[string]*Role `json:"roles"`
102+
Custom *json.RawMessage `json:"custom,omitempty"`
102103

103104
ConsistentSnapshot bool `json:"consistent_snapshot"`
104105
}
@@ -171,11 +172,12 @@ type SnapshotFileMeta struct {
171172
type SnapshotFiles map[string]SnapshotFileMeta
172173

173174
type Snapshot struct {
174-
Type string `json:"_type"`
175-
SpecVersion string `json:"spec_version"`
176-
Version int `json:"version"`
177-
Expires time.Time `json:"expires"`
178-
Meta SnapshotFiles `json:"meta"`
175+
Type string `json:"_type"`
176+
SpecVersion string `json:"spec_version"`
177+
Version int `json:"version"`
178+
Expires time.Time `json:"expires"`
179+
Meta SnapshotFiles `json:"meta"`
180+
Custom *json.RawMessage `json:"custom,omitempty"`
179181
}
180182

181183
func NewSnapshot() *Snapshot {
@@ -198,12 +200,13 @@ func (f TargetFileMeta) HashAlgorithms() []string {
198200
}
199201

200202
type Targets struct {
201-
Type string `json:"_type"`
202-
SpecVersion string `json:"spec_version"`
203-
Version int `json:"version"`
204-
Expires time.Time `json:"expires"`
205-
Targets TargetFiles `json:"targets"`
206-
Delegations *Delegations `json:"delegations,omitempty"`
203+
Type string `json:"_type"`
204+
SpecVersion string `json:"spec_version"`
205+
Version int `json:"version"`
206+
Expires time.Time `json:"expires"`
207+
Targets TargetFiles `json:"targets"`
208+
Delegations *Delegations `json:"delegations,omitempty"`
209+
Custom *json.RawMessage `json:"custom,omitempty"`
207210
}
208211

209212
// Delegations represents the edges from a parent Targets role to one or more
@@ -304,11 +307,12 @@ type TimestampFileMeta struct {
304307
type TimestampFiles map[string]TimestampFileMeta
305308

306309
type Timestamp struct {
307-
Type string `json:"_type"`
308-
SpecVersion string `json:"spec_version"`
309-
Version int `json:"version"`
310-
Expires time.Time `json:"expires"`
311-
Meta TimestampFiles `json:"meta"`
310+
Type string `json:"_type"`
311+
SpecVersion string `json:"spec_version"`
312+
Version int `json:"version"`
313+
Expires time.Time `json:"expires"`
314+
Meta TimestampFiles `json:"meta"`
315+
Custom *json.RawMessage `json:"custom,omitempty"`
312316
}
313317

314318
func NewTimestamp() *Timestamp {

data/types_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,3 +239,49 @@ func TestDelegatedRoleUnmarshalErr(t *testing.T) {
239239
err := json.Unmarshal([]byte(`{"keyids":"a"}`), &d)
240240
assert.Equal(t, "keyids", err.(*json.UnmarshalTypeError).Field)
241241
}
242+
243+
func TestCustomField(t *testing.T) {
244+
testCustomJSON := json.RawMessage([]byte(`{"test":true}`))
245+
246+
root := Root{
247+
Type: "root",
248+
SpecVersion: "1.0",
249+
Keys: make(map[string]*PublicKey),
250+
Roles: make(map[string]*Role),
251+
ConsistentSnapshot: true,
252+
Custom: &testCustomJSON,
253+
}
254+
rootJSON, err := json.Marshal(&root)
255+
assert.NoError(t, err)
256+
assert.Equal(t, []byte("{\"_type\":\"root\",\"spec_version\":\"1.0\",\"version\":0,\"expires\":\"0001-01-01T00:00:00Z\",\"keys\":{},\"roles\":{},\"custom\":{\"test\":true},\"consistent_snapshot\":true}"), rootJSON)
257+
258+
targets := Targets{
259+
Type: "targets",
260+
SpecVersion: "1.0",
261+
Targets: make(TargetFiles),
262+
Custom: &testCustomJSON,
263+
}
264+
targetsJSON, err := json.Marshal(&targets)
265+
assert.NoError(t, err)
266+
assert.Equal(t, []byte("{\"_type\":\"targets\",\"spec_version\":\"1.0\",\"version\":0,\"expires\":\"0001-01-01T00:00:00Z\",\"targets\":{},\"custom\":{\"test\":true}}"), targetsJSON)
267+
268+
snapshot := Snapshot{
269+
Type: "snapshot",
270+
SpecVersion: "1.0",
271+
Meta: make(SnapshotFiles),
272+
Custom: &testCustomJSON,
273+
}
274+
snapshotJSON, err := json.Marshal(&snapshot)
275+
assert.NoError(t, err)
276+
assert.Equal(t, []byte("{\"_type\":\"snapshot\",\"spec_version\":\"1.0\",\"version\":0,\"expires\":\"0001-01-01T00:00:00Z\",\"meta\":{},\"custom\":{\"test\":true}}"), snapshotJSON)
277+
278+
timestamp := Timestamp{
279+
Type: "timestamp",
280+
SpecVersion: "1.0",
281+
Meta: make(TimestampFiles),
282+
Custom: &testCustomJSON,
283+
}
284+
timestampJSON, err := json.Marshal(&timestamp)
285+
assert.NoError(t, err)
286+
assert.Equal(t, []byte("{\"_type\":\"timestamp\",\"spec_version\":\"1.0\",\"version\":0,\"expires\":\"0001-01-01T00:00:00Z\",\"meta\":{},\"custom\":{\"test\":true}}"), timestampJSON)
287+
}

0 commit comments

Comments
 (0)