-
Notifications
You must be signed in to change notification settings - Fork 328
/
cmd_extras.go
105 lines (93 loc) · 2.68 KB
/
cmd_extras.go
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright (C) 2017 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package api
import "github.com/google/gapid/core/data/deep"
// CmdExtra is the interface implemented by command 'extras' - additional
// information that can be placed inside a command.
type CmdExtra interface{}
// CmdExtras is a list of CmdExtra objects.
type CmdExtras []CmdExtra
func (e *CmdExtras) All() CmdExtras {
if e == nil {
return nil
}
return *e
}
// Add appends one or more CmdExtras to the list of CmdExtras.
func (e *CmdExtras) Add(es ...CmdExtra) {
if e != nil {
*e = append(*e, es...)
}
}
// Replace replaces the `old` CmdExtra with the `new` CmdExtra.
// If `old` is not found, this does nothing.
func (e *CmdExtras) Replace(old, new CmdExtra) {
if e != nil {
for i, x := range *e {
if x == old {
(*e)[i] = new
return
}
}
}
}
// MustClone clones all of es, adding them to e.
// if there was an error, a panic is raised
func (e *CmdExtras) MustClone(es ...CmdExtra) {
if e != nil {
for _, ex := range es {
i, err := deep.Clone(ex)
if err != nil {
panic(err)
}
*e = append(*e, i)
}
}
}
// Aborted returns a pointer to the ErrCmdAborted structure in the CmdExtras, or
// nil if not found.
func (e *CmdExtras) Aborted() *ErrCmdAborted {
for _, e := range e.All() {
if e, ok := e.(*ErrCmdAborted); ok {
return e
}
}
return nil
}
// Observations returns a pointer to the CmdObservations structure in the
// CmdExtras, or nil if there are no observations in the CmdExtras.
func (e *CmdExtras) Observations() *CmdObservations {
for _, o := range e.All() {
if o, ok := o.(*CmdObservations); ok {
return o
}
}
return nil
}
// GetOrAppendObservations returns a pointer to the existing Observations
// structure in the CmdExtras, or appends and returns a pointer to a new
// observations structure if the CmdExtras does not already contain one.
func (e *CmdExtras) GetOrAppendObservations() *CmdObservations {
if o := e.Observations(); o != nil {
return o
}
o := &CmdObservations{}
e.Add(o)
return o
}
// WithExtras adds the given extras to a command and returns it.
func WithExtras(a Cmd, extras ...CmdExtra) Cmd {
a.Extras().Add(extras...)
return a
}