request.Do: use pointer to bool (#1632)

Drop the doTrue and doFalse and use a pointer to a bool to do a proper
tri-bool.
This commit is contained in:
Miek Gieben 2018-03-31 17:22:24 +01:00 committed by GitHub
parent fd1501e918
commit 5c5a98ee29
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 22 deletions

View file

@ -23,7 +23,7 @@ type Request struct {
// Cache size after first call to Size or Do. // Cache size after first call to Size or Do.
size int size int
do int // 0: not, 1: true: 2: false do *bool // nil: nothing, otherwise *do value
// TODO(miek): opt record itself as well? // TODO(miek): opt record itself as well?
// Cache lowercase qname. // Cache lowercase qname.
@ -95,19 +95,17 @@ func (r *Request) Family() int {
// Do returns if the request has the DO (DNSSEC OK) bit set. // Do returns if the request has the DO (DNSSEC OK) bit set.
func (r *Request) Do() bool { func (r *Request) Do() bool {
if r.do != 0 { if r.do != nil {
return r.do == doTrue return *r.do
} }
r.do = new(bool)
if o := r.Req.IsEdns0(); o != nil { if o := r.Req.IsEdns0(); o != nil {
if o.Do() { *r.do = o.Do()
r.do = doTrue return *r.do
} else {
r.do = doFalse
}
return o.Do()
} }
r.do = doFalse *r.do = false
return false return false
} }
@ -123,14 +121,13 @@ func (r *Request) Size() int {
size := 0 size := 0
if o := r.Req.IsEdns0(); o != nil { if o := r.Req.IsEdns0(); o != nil {
if o.Do() { if r.do == nil {
r.do = doTrue r.do = new(bool)
} else {
r.do = doFalse
} }
*r.do = o.Do()
size = int(o.UDPSize()) size = int(o.UDPSize())
} }
// TODO(miek) move edns.Size to dnsutil?
size = edns.Size(r.Proto(), size) size = edns.Size(r.Proto(), size)
r.size = size r.size = size
return size return size
@ -389,9 +386,4 @@ func (r *Request) Match(reply *dns.Msg) bool {
return true return true
} }
const ( const optLen = 12 // OPT record length.
// TODO(miek): make this less awkward.
doTrue = 1
doFalse = 2
optLen = 12 // OPT record length.
)

View file

@ -13,7 +13,7 @@ func TestRequestDo(t *testing.T) {
st := testRequest() st := testRequest()
st.Do() st.Do()
if st.do == 0 { if st.do == nil {
t.Fatalf("Expected st.do to be set") t.Fatalf("Expected st.do to be set")
} }
} }