-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdbc.go
176 lines (156 loc) · 3.68 KB
/
dbc.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
//+build !without_dbc
package dbc
import (
"errors"
"fmt"
"runtime"
)
// Contract structure to hold info about Logger
type Contract struct {
log Logger
}
// Create new contract without logging
func New() *Contract {
return &Contract{
log: nil,
}
}
// Create new contract with logging
func NewWithLogger(log Logger) *Contract {
return &Contract{
log: log,
}
}
func _panic(contract *Contract, function string, condition bool, msgs ...interface{}) {
if condition {
return
}
caller := ""
pc, file, line, ok := runtime.Caller(2)
funcInfo := runtime.FuncForPC(pc)
if ok {
caller = fmt.Sprintf("func %s in %s at %d",
funcInfo.Name(),
file,
line,
)
} else {
caller = "unknown caller"
}
str := fmt.Sprintf("%s: %s", function, caller)
if contract.log != nil {
contract.log.Debug(str)
}
for i := range msgs {
s := fmt.Sprintf("[%d] %+v", i, msgs[i])
str += "\n" + s
if contract.log != nil {
contract.log.Debug(s)
}
}
panic(errors.New(str))
}
// Requires check preconditions
//
// ...
// contract := dbc.New()
// ...
//
// func f(parm1 int, parm2 int) {
// contract.Require(parm1 != parm2)
// ...
// }
//
func (contract *Contract) Require(condition bool, msgs ...interface{}) {
_panic(contract, "Require", condition, msgs)
}
// Requires check postconditions
//
// ...
// contract := dbc.New()
// ...
//
// func f() string {
// ..
// value, ok := someMap[key]
// contract.Ensure(ok)
// return value
// }
//
func (contract *Contract) Ensure(condition bool, msgs ...interface{}) {
_panic(contract, "Ensure", condition, msgs)
}
// Simple assert
func (contract *Contract) Assert(condition bool, msgs ...interface{}) {
_panic(contract, "Assert", condition, msgs)
}
// Simple assert, but "Check" name
func (contract *Contract) Check(condition bool, msgs ...interface{}) {
_panic(contract, "Check", condition, msgs)
}
// SimpleInvariant use SimpleInvariantValidator interface for check conditions.
/*
...
type T1 struct {
f bool
}
func (t1 T1) Invariant() bool {
return t1.f
}
...
contract := New()
...
t1 := T1{
f: false,
}
...
contract.SimpleInvariant(t1)
*/
func (contract *Contract) SimpleInvariant(validatedObject SimpleInvariantValidator, msgs ...interface{}) {
_panic(contract, "Invariant", validatedObject.Invariant(), msgs)
}
// Invariant use Validator interface (with Stringer) for check conditions.
/*
...
type T1 struct {
f bool
}
func (t1 T1) Invariant() bool {
return t1.f
}
func (t1 T1) String() string {
return fmt.Sprintf("T1.f: %t", t1.f)
}
...
contract := New()
...
t1 := T1{
f: false,
}
...
contract.Invariant(t1)
*/
func (contract *Contract) Invariant(validatedObject InvariantValidator, msgs ...interface{}) {
_panic(contract, "Invariant", validatedObject.Invariant(),
append(msgs, validatedObject))
}
var defaultContract = New()
func Require(condition bool, msgs ...interface{}) {
_panic(defaultContract, "Require", condition, msgs)
}
func Ensure(condition bool, msgs ...interface{}) {
_panic(defaultContract, "Ensure", condition, msgs)
}
func Assert(condition bool, msgs ...interface{}) {
_panic(defaultContract, "Assert", condition, msgs)
}
func Check(condition bool, msgs ...interface{}) {
_panic(defaultContract, "Check", condition, msgs)
}
func SimpleInvariant(validatedObject SimpleInvariantValidator, msgs ...interface{}) {
_panic(defaultContract, "Invariant", validatedObject.Invariant(), msgs)
}
func Invariant(validatedObject InvariantValidator, msgs ...interface{}) {
_panic(defaultContract, "Invariant", validatedObject.Invariant(),
append(msgs, validatedObject))
}