Commit 585938ed authored by Sietse Ringers's avatar Sietse Ringers
Browse files

refactor: simplify attribute request marshaling code

parent f1a0344e
...@@ -233,56 +233,37 @@ func (c AttributeCon) Validate() error { ...@@ -233,56 +233,37 @@ func (c AttributeCon) Validate() error {
return nil return nil
} }
func (c *AttributeCon) MarshalJSON() ([]byte, error) { // AttributeRequest synonym with default JSON (un)marshaler
var vals bool type jsonAttributeRequest AttributeRequest
l := make([]AttributeTypeIdentifier, 0, len(*c))
for _, attr := range *c { func (ar *AttributeRequest) UnmarshalJSON(bts []byte) error {
if attr.Value == nil { var s AttributeTypeIdentifier
l = append(l, attr.Type)
} else {
vals = true
break
}
}
if vals {
return json.Marshal((*[]AttributeRequest)(c))
} else {
return json.Marshal(l)
}
}
func (c *AttributeCon) UnmarshalJSON(bts []byte) error { // first try to parse as JSON string into s
var err error if err := json.Unmarshal(bts, &s); err == nil {
*ar = AttributeRequest{Type: s}
var l []AttributeTypeIdentifier
if err = json.Unmarshal(bts, &l); err == nil {
for _, id := range l {
*c = append(*c, AttributeRequest{Type: id})
}
return nil return nil
} }
if err = json.Unmarshal(bts, (*[]AttributeRequest)(c)); err == nil { return json.Unmarshal(bts, (*jsonAttributeRequest)(ar))
return nil }
}
var s string func (ar *AttributeRequest) MarshalJSON() ([]byte, error) {
if err = json.Unmarshal(bts, &s); err == nil { if !ar.Required && ar.Value == nil {
*c = append(*c, NewAttributeRequest(s)) return json.Marshal(ar.Type)
return nil
} }
return json.Marshal((*jsonAttributeRequest)(ar))
return errors.New("Failed to unmarshal attribute conjunction")
} }
// Satisfy indicates whether the given attribute type and value satisfies this AttributeRequest.
func (ar *AttributeRequest) Satisfy(attr AttributeTypeIdentifier, val *string) bool { func (ar *AttributeRequest) Satisfy(attr AttributeTypeIdentifier, val *string) bool {
return ar.Type == attr && return ar.Type == attr &&
(!ar.Required || val != nil) && (!ar.Required || val != nil) &&
(ar.Value == nil || (val != nil && *ar.Value == *val)) (ar.Value == nil || (val != nil && *ar.Value == *val))
} }
// Satisfy returns if each of the attributes specified by proofs and indices satisfies each of
// the contained AttributeRequests's. If so it also returns a list of the disclosed attribute values.
func (c AttributeCon) Satisfy(proofs gabi.ProofList, indices []*DisclosedAttributeIndex, conf *Configuration) (bool, []*DisclosedAttribute, error) { func (c AttributeCon) Satisfy(proofs gabi.ProofList, indices []*DisclosedAttributeIndex, conf *Configuration) (bool, []*DisclosedAttribute, error) {
if len(indices) < len(c) { if len(indices) < len(c) {
return false, nil, nil return false, nil, nil
...@@ -319,6 +300,8 @@ func (dc AttributeDisCon) Validate() error { ...@@ -319,6 +300,8 @@ func (dc AttributeDisCon) Validate() error {
return nil return nil
} }
// Satisfy returns true if the attributes specified by proofs and indices satisfies any one of the
// contained AttributeCon's. If so it also returns a list of the disclosed attribute values.
func (dc AttributeDisCon) Satisfy(proofs gabi.ProofList, indices []*DisclosedAttributeIndex, conf *Configuration) (bool, []*DisclosedAttribute, error) { func (dc AttributeDisCon) Satisfy(proofs gabi.ProofList, indices []*DisclosedAttributeIndex, conf *Configuration) (bool, []*DisclosedAttribute, error) {
for _, con := range dc { for _, con := range dc {
satisfied, attrs, err := con.Satisfy(proofs, indices, conf) satisfied, attrs, err := con.Satisfy(proofs, indices, conf)
...@@ -348,6 +331,8 @@ func (cdc AttributeConDisCon) Validate(conf *Configuration) error { ...@@ -348,6 +331,8 @@ func (cdc AttributeConDisCon) Validate(conf *Configuration) error {
return nil return nil
} }
// Satisfy returns true if each of the contained AttributeDisCon is satisfied by the specified disclosure.
// If so it also returns the disclosed attributes.
func (cdc AttributeConDisCon) Satisfy(disclosure *Disclosure, conf *Configuration) (bool, [][]*DisclosedAttribute, error) { func (cdc AttributeConDisCon) Satisfy(disclosure *Disclosure, conf *Configuration) (bool, [][]*DisclosedAttribute, error) {
if len(disclosure.Indices) < len(cdc) { if len(disclosure.Indices) < len(cdc) {
return false, nil, nil return false, nil, nil
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment