Skip to content
This repository has been archived by the owner on Dec 1, 2024. It is now read-only.

Commit

Permalink
Improved error handler (experimental), interface Identifier convenien…
Browse files Browse the repository at this point in the history
…ce method
  • Loading branch information
Jesse Coretta authored and Jesse Coretta committed Nov 12, 2024
1 parent a46937a commit 9fc7ee2
Show file tree
Hide file tree
Showing 17 changed files with 727 additions and 15 deletions.
52 changes: 51 additions & 1 deletion at.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,19 @@ func (r AttributeType) Marshal(def any) error {
return nil
}

/*
E returns the underlying error instance.
*/
func (r AttributeType) E() (err error) {
if !r.IsZero() {
err = r.attributeType.err
} else {
err = ErrNilReceiver
}

return
}

func (r AttributeType) marshalExt(key string, v any) {
if hasPfx(key, `X-`) {
switch tv := v.(type) {
Expand Down Expand Up @@ -234,8 +247,10 @@ func (r AttributeType) Replace(x AttributeType) AttributeType {

func (r *attributeType) replace(x AttributeType) {
if r.OID == `` {
r.err = ErrMissingNumericOID
return
} else if r.OID != x.NumericOID() {
r.err = ErrInvalidOID
return
}

Expand Down Expand Up @@ -713,6 +728,13 @@ func (r AttributeType) IsZero() bool {
return r.attributeType == nil
}

/*
Identifier returns the output of [AttributeType.OID] and is merely used
for an interface-friendly means of obtaining the principal identifier
of the receiver instance.
*/
func (r AttributeType) Identifier() (ident string) { return r.OID() }

/*
IsIdentifiedAs returns a Boolean value indicative of whether id matches
either the numericOID or descriptor of the receiver instance. Case is
Expand Down Expand Up @@ -864,6 +886,8 @@ func (r *attributeType) setNumericOID(id string) {
if len(r.OID) == 0 {
r.OID = id
}
} else {
r.err = ErrInvalidOID
}

return
Expand Down Expand Up @@ -916,9 +940,14 @@ func (r AttributeType) SetName(x ...string) AttributeType {
}

func (r *attributeType) setName(x ...string) {
b4 := r.Name.Len()
for i := 0; i < len(x); i++ {
r.Name.Push(x[i])
}

if r.Name.Len()-len(x) != b4 {
r.err = ErrInvalidNames
}
}

/*
Expand Down Expand Up @@ -1039,6 +1068,8 @@ func (r *attributeType) setStringer(function ...Stringer) {
// Return a static value.
return str
}
} else {
r.err = err
}
return
}
Expand Down Expand Up @@ -1100,6 +1131,10 @@ func (r *attributeType) prepareString() (str string, err error) {
}
}

if err != nil {
r.err = err
}

return
}

Expand Down Expand Up @@ -1221,6 +1256,8 @@ func (r *attributeType) setSyntax(x any) {

if def.Compliant() {
r.Syntax = def
} else {
r.err = ErrInvalidSyntax
}
}

Expand Down Expand Up @@ -1295,6 +1332,8 @@ func (r *attributeType) setEquality(x any) {

if def.Compliant() {
r.Equality = def
} else {
r.err = ErrEqualityRuleNotFound
}
}

Expand Down Expand Up @@ -1369,6 +1408,8 @@ func (r *attributeType) setSubstring(x any) {

if def.Compliant() {
r.Substring = def
} else {
r.err = ErrSubstringRuleNotFound
}
}

Expand Down Expand Up @@ -1443,6 +1484,8 @@ func (r *attributeType) setOrdering(x any) {

if def.Compliant() {
r.Ordering = def
} else {
r.err = ErrOrderingRuleNotFound
}
}

Expand Down Expand Up @@ -1538,6 +1581,8 @@ func (r *attributeType) setSuperType(x any) {

if def.Compliant() {
r.SuperType = def
} else {
r.err = ErrSuperTypeNotFound
}
}

Expand Down Expand Up @@ -1808,7 +1853,12 @@ func (r AttributeType) Compliant() bool {

// Any combination of SV/C is permitted
// EXCEPT for BOTH. See RFC 3671.
return !(r.SingleValue() && collective)
ok := !(r.SingleValue() && collective)
if ok {
r.attributeType.err = nil
}

return ok
}

/*
Expand Down
38 changes: 38 additions & 0 deletions at_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,44 @@ func ExampleAttributeType_SubTypes() {
// Output: 15 subordinate types found
}

/*
This example demonstrates the means for checking to see if the receiver
is in an error condition.
*/
func ExampleAttributeType_E() {
def := mySchema.NewAttributeType()
def.SetNumericOID(`23jklm5.1`) // bogus
if err := def.E(); err != nil {
fmt.Println(err)
}
// Output: Numeric OID is invalid
}

/*
This example demonstrates the means for resolving an error condition.
*/
func ExampleAttributeType_E_clearError() {
def := mySchema.NewAttributeType()
def.SetNumericOID(`23jklm5.1`) // bogus

// We realized our mistake.
def.SetNumericOID(`1.3.6.1.4.1.56521.999.8.4.1.1`) // valid

// But when we check again, the error is still there.
if def.E() != nil {
//fmt.Println(... the error ...)
}

// We must clear the error with a
// passing compliance check.
if def.Compliant(); def.E() == nil {
fmt.Println("Error has been resolved")
}
// Output: Error has been resolved

return
}

/*
This example demonstrates a compliancy check of the "name" [AttributeType].
Expand Down
64 changes: 62 additions & 2 deletions dc.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@ func (r DITContentRule) Description() (desc string) {
return
}

/*
E returns the underlying error instance.
*/
func (r DITContentRule) E() (err error) {
if !r.IsZero() {
err = r.dITContentRule.err
} else {
err = ErrNilReceiver
}

return
}

/*
DITContentRules returns the [DITContentRules] instance from within
the receiver instance.
Expand Down Expand Up @@ -343,6 +356,13 @@ func (r DITContentRules) oIDsStringer(_ ...any) (present string) {
return
}

/*
Identifier returns the output of [DITContentRule.OID] and is merely used
for an interface-friendly means of obtaining the principal identifier
of the receiver instance.
*/
func (r DITContentRule) Identifier() (ident string) { return r.OID() }

/*
IsIdentifiedAs returns a Boolean value indicative of whether id matches
either the numericOID or descriptor of the receiver instance. Case is
Expand Down Expand Up @@ -425,6 +445,10 @@ func (r *dITContentRule) setAux(m ...any) {
r.Aux.Push(oc)
}
}

if err != nil {
r.err = err
}
}

/*
Expand Down Expand Up @@ -522,7 +546,12 @@ func (r DITContentRule) Compliant() bool {
// if any dITStructureRule definitions exist,
// make sure they don't produce a MUST/NOT
// conflict.
return r.dsrComply(structural)
ok := r.dsrComply(structural)
if ok {
r.dITContentRule.err = nil
}

return ok
}

func (r DITContentRule) dsrComply(structural ObjectClass) bool {
Expand Down Expand Up @@ -782,9 +811,14 @@ func (r DITContentRule) SetName(x ...string) DITContentRule {
}

func (r *dITContentRule) setName(x ...string) {
b4 := r.Name.Len()
for i := 0; i < len(x); i++ {
r.Name.Push(x[i])
}

if r.Name.Len()-len(x) != b4 {
r.err = ErrInvalidNames
}
}

/*
Expand Down Expand Up @@ -882,8 +916,12 @@ func (r *dITContentRule) setNumericOID(id string) {
oc := r.schema.ObjectClasses().Get(id)
if oc.Kind() == StructuralKind {
r.OID = oc
} else {
r.err = ErrIncompatStructuralClass
}
}
} else {
r.err = ErrInvalidOID
}

return
Expand Down Expand Up @@ -932,6 +970,10 @@ func (r *dITContentRule) setMust(m ...any) {
r.Must.Push(at)
}
}

if err != nil {
r.err = err
}
}

/*
Expand Down Expand Up @@ -977,6 +1019,10 @@ func (r *dITContentRule) setMay(m ...any) {
r.May.Push(at)
}
}

if err != nil {
r.err = err
}
}

/*
Expand Down Expand Up @@ -1022,6 +1068,10 @@ func (r *dITContentRule) setNot(m ...any) {
r.Not.Push(at)
}
}

if err != nil {
r.err = err
}
}

/*
Expand Down Expand Up @@ -1083,6 +1133,8 @@ func (r *dITContentRule) setStringer(function ...Stringer) {
// Return a preserved value.
return str
}
} else {
r.err = err
}
} else {
r.stringer = stringer
Expand Down Expand Up @@ -1173,6 +1225,10 @@ func (r *dITContentRule) prepareString() (str string, err error) {
}
}

if err != nil {
r.err = err
}

return
}

Expand Down Expand Up @@ -1344,7 +1400,11 @@ func (r DITContentRule) Replace(x DITContentRule) DITContentRule {
}

func (r *dITContentRule) replace(x DITContentRule) {
if r.OID.NumericOID() != x.NumericOID() {
if r.OID.NumericOID() == `` {
r.err = ErrMissingNumericOID
return
} else if r.OID.NumericOID() != x.NumericOID() {
r.err = ErrInvalidOID
return
}

Expand Down
38 changes: 38 additions & 0 deletions dc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,44 @@ func ExampleDITContentRule_Marshal() {
// X-ORIGIN 'RFCXXXX' )
}

/*
This example demonstrates the means for checking to see if the receiver
is in an error condition.
*/
func ExampleDITContentRule_E() {
def := mySchema.NewDITContentRule()
def.SetNumericOID(`23jklm5.1`) // bogus
if err := def.E(); err != nil {
fmt.Println(err)
}
// Output: Numeric OID is invalid
}

/*
This example demonstrates the means for resolving an error condition.
*/
func ExampleDITContentRule_E_clearError() {
def := mySchema.NewDITContentRule()
def.SetNumericOID(`23jklm5.1`) // bogus

// We realized our mistake.
def.SetNumericOID(mySchema.ObjectClasses().Get(`person`).NumericOID()) // valid

// But when we check again, the error is still there.
if def.E() != nil {
//fmt.Println(... the error ...)
}

// We must clear the error with a
// passing compliance check.
if def.Compliant(); def.E() == nil {
fmt.Println("Error has been resolved")
}
// Output: Error has been resolved

return
}

/*
This example demonstrates a compliancy check of the "account" [ObjectClass].
*/
Expand Down
Loading

0 comments on commit 9fc7ee2

Please sign in to comment.