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

Commit

Permalink
begin throwing errors on duplicate entries
Browse files Browse the repository at this point in the history
  • Loading branch information
Jesse Coretta authored and Jesse Coretta committed Nov 12, 2024
1 parent f30b848 commit a46937a
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 38 deletions.
16 changes: 8 additions & 8 deletions at_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -884,8 +884,8 @@ in place of the fictional "mySchema" var shown here for simplicity.
*/
func ExampleAttributeType_SetSyntax() {
// First we'll craft a fake attribute
raw := `( 1.3.6.1.4.1.56521.999.14.56.1
NAME 'coolattr'
raw := `( 1.3.6.1.4.1.56521.999.14.56.12
NAME 'coolattr2'
EQUALITY caseIgnoreMatch )`

var attr AttributeType = mySchema.NewAttributeType()
Expand All @@ -911,8 +911,8 @@ in place of the fictional "mySchema" var shown here for simplicity.
*/
func ExampleAttributeType_SetEquality() {
// First we'll craft a fake attribute
raw := `( 1.3.6.1.4.1.56521.999.14.56.1
NAME 'coolattr'
raw := `( 1.3.6.1.4.1.56521.999.14.56.13
NAME 'coolattr3'
SYNTAX 1.3.6.1.4.1.1466.115.121.1.26 )`

var attr AttributeType = mySchema.NewAttributeType()
Expand All @@ -938,8 +938,8 @@ in place of the fictional "mySchema" var shown here for simplicity.
*/
func ExampleAttributeType_SetSubstring() {
// First we'll craft a fake attribute
raw := `( 1.3.6.1.4.1.56521.999.14.56.1
NAME 'coolattr'
raw := `( 1.3.6.1.4.1.56521.999.14.56.14
NAME 'coolattr4'
SYNTAX 1.3.6.1.4.1.1466.115.121.1.26 )`

var attr AttributeType = mySchema.NewAttributeType()
Expand All @@ -965,8 +965,8 @@ in place of the fictional "mySchema" var shown here for simplicity.
*/
func ExampleAttributeType_SetOrdering() {
// First we'll craft a fake attribute
raw := `( 1.3.6.1.4.1.56521.999.14.56.1
NAME 'coolattr'
raw := `( 1.3.6.1.4.1.56521.999.14.56.15
NAME 'coolattr5'
SYNTAX 1.3.6.1.4.1.1466.115.121.1.27 )`

var attr AttributeType = mySchema.NewAttributeType()
Expand Down
9 changes: 4 additions & 5 deletions dc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,8 @@ func ExampleDITContentRule_SetData() {
// use a string for simplicity.
documentation := `Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.`

// Obtain the target attribute type to bear
// the assigned value.
dvc := mySchema.DITContentRules().Index(0)
// Pretend this is a real dITContentRule
dvc := mySchema.NewDITContentRule()

// Set it.
dvc.SetData(documentation)
Expand Down Expand Up @@ -398,8 +397,8 @@ func TestDITContentRule_codecov(t *testing.T) {
def.SetNot(mySchema.AttributeTypes().Get(`l`))
def.Map()
mySchema.DITContentRules().canPush(DITContentRule{}, DITContentRule{new(dITContentRule)})
if err := def.Parse(raw); err != nil {
t.Errorf("%s failed: expected success, got %v", t.Name(), err)
if err := def.Parse(raw); err != ErrDuplicateDef {
t.Errorf("%s failed: expected duplicate err, got %v", t.Name(), err)
return
}
def.StructuralClass().EnforcedBy()
Expand Down
22 changes: 11 additions & 11 deletions ds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ in place of the fictional "mySchema" var shown here for simplicity.
*/
func ExampleDITStructureRule_Compliant() {
// grab our dITStructureRule bearing an ID of zero (0)
ds := mySchema.DITStructureRules().Get(0) // or "rootArcStructure"
ds := mySchema.DITStructureRules().Get(20) // or "rootArcStructure"
fmt.Println(ds.Compliant())
// Output: true
}
Expand Down Expand Up @@ -148,8 +148,8 @@ in place of the fictional "mySchema" var shown here for simplicity.
func ExampleDITStructureRules_XOrigin() {
defs := mySchema.DITStructureRules()
matches := defs.XOrigin(`RFC4403`)
fmt.Printf("Matched %d of %d %s\n", matches.Len(), defs.Len(), defs.Type())
// Output: Matched 10 of 15 dITStructureRules
fmt.Printf("Matched %d %s\n", matches.Len(), defs.Type())
// Output: Matched 10 dITStructureRules
}

/*
Expand Down Expand Up @@ -202,7 +202,7 @@ Note: this example assumes a legitimate schema variable is defined
in place of the fictional "mySchema" var shown here for simplicity.
*/
func ExampleDITStructureRule_ID() {
def := mySchema.DITStructureRules().Get(0)
def := mySchema.DITStructureRules().Get(20)
fmt.Println(def.ID())
// Output: rootArcStructure
}
Expand All @@ -217,7 +217,7 @@ in place of the fictional "mySchema" var shown here for simplicity.
func ExampleDITStructureRule_RuleID() {
def := mySchema.DITStructureRules().Get(`arcStructure`) // or 11, or "11"
fmt.Println(def.RuleID())
// Output: 11
// Output: 0
}

/*
Expand All @@ -228,7 +228,7 @@ Note: this example assumes a legitimate schema variable is defined
in place of the fictional "mySchema" var shown here for simplicity.
*/
func ExampleDITStructureRule_Name() {
def := mySchema.DITStructureRules().Get(0)
def := mySchema.DITStructureRules().Get(20)
fmt.Println(def.Name())
// Output: rootArcStructure
}
Expand Down Expand Up @@ -286,9 +286,9 @@ method and may return zero (0) or more [DITStructureRule] instances within
the return [DITStructureRules] instance.
*/
func ExampleDITStructureRule_SubRules() {
def := mySchema.DITStructureRules().Get(0)
fmt.Printf("%d subordinate rules found", def.SubRules().Len())
// Output: 2 subordinate rules found
def := mySchema.DITStructureRules().Get(5)
fmt.Printf("%d subordinate rule found", def.SubRules().Len())
// Output: 1 subordinate rule found
}

/*
Expand All @@ -314,7 +314,7 @@ Note: this example assumes a legitimate schema variable is defined
in place of the fictional "mySchema" var shown here for simplicity.
*/
func ExampleDITStructureRule_Map() {
def := mySchema.DITStructureRules().Get(0)
def := mySchema.DITStructureRules().Get(20)
fmt.Println(def.Map()[`NAME`][0])
// Output: rootArcStructure
}
Expand Down Expand Up @@ -363,7 +363,7 @@ func ExampleDITStructureRule_SetData() {
}

func ExampleDITStructureRule_IsIdentifiedAs() {
def := mySchema.DITStructureRules().Get(0)
def := mySchema.DITStructureRules().Get(20)
fmt.Println(def.IsIdentifiedAs(`rootArcStructure`))
// Output: true
}
Expand Down
1 change: 1 addition & 0 deletions err.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var (
ErrNamingViolationUnsanctioned error = errors.New("Naming violation; unsanctioned attribute type used")
ErrNamingViolationChildlessSSR error = errors.New("Naming violation; childless superior structure rule")
ErrNamingViolationBadClassAttr error = errors.New("Naming violation; named object class does not facilitate one or more attribute types present")
ErrDuplicateDef error = errors.New("Cannot parse or load; duplicate definition")
ErrNilSyntaxQualifier error = errors.New("No SyntaxQualifier instance assigned to LDAPSyntax")
ErrNilValueQualifier error = errors.New("No ValueQualifier instance assigned to AttributeType")
ErrNilAssertionMatcher error = errors.New("No AssertionMatcher instance assigned to MatchingRule")
Expand Down
4 changes: 2 additions & 2 deletions mr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -515,8 +515,8 @@ func TestMatchingRule_codecov(t *testing.T) {
def.SetData(nil)
def.Data()

if err := def.Parse(raw); err != nil {
t.Errorf("%s failed: expected success, got %v", t.Name(), err)
if err := def.Parse(raw); err != ErrDuplicateDef {
t.Errorf("%s failed: expected duplicate err, got %v", t.Name(), err)
return
}
_ = def.macro()
Expand Down
49 changes: 43 additions & 6 deletions parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,9 @@ func (r Schema) marshalLS(s antlr4512.LDAPSyntax) (def LDAPSyntax, err error) {
return
}

// silently ignore attempts to marshal a duplicate definition.
// fail attempts to marshal a duplicate definition.
if lup := r.LDAPSyntaxes().get(s.OID); !lup.IsZero() {
err = ErrDuplicateDef
return
}

Expand Down Expand Up @@ -248,6 +249,12 @@ func (r Schema) marshalMR(s antlr4512.MatchingRule) (def MatchingRule, err error
}
}

// fail attempts to marshal a duplicate definition.
if lup := r.MatchingRules().get(s.OID); !lup.IsZero() {
err = ErrDuplicateDef
return
}

if !isNumericOID(s.OID) {
err = ErrMissingNumericOID
return
Expand Down Expand Up @@ -323,6 +330,12 @@ func (r Schema) marshalMU(s antlr4512.MatchingRuleUse) (def MatchingRuleUse, err
return
}

// fail attempts to marshal a duplicate definition.
if lup := r.MatchingRuleUses().get(s.OID); !lup.IsZero() {
err = ErrDuplicateDef
return
}

_def := newMatchingRuleUse()
_def.OID = lup
_def.Desc = s.Desc
Expand Down Expand Up @@ -380,7 +393,8 @@ func (r Schema) marshalAT(s antlr4512.AttributeType) (def AttributeType, err err
err = ErrMissingNumericOID
return
} else if lup := r.AttributeTypes().get(s.OID); !lup.IsZero() {
// silently ignore attempts to marshal a duplicate definition.
// fail on attempts to marshal a duplicate definition.
err = ErrDuplicateDef
return
}

Expand Down Expand Up @@ -520,8 +534,9 @@ func (r Schema) marshalOC(s antlr4512.ObjectClass) (def ObjectClass, err error)
return
}

// silently ignore attempts to marshal a duplicate definition.
// fail attempts to marshal a duplicate definition.
if lup := r.ObjectClasses().get(s.OID); !lup.IsZero() {
err = ErrDuplicateDef
return
}

Expand Down Expand Up @@ -607,6 +622,17 @@ func (r Schema) incorporateDC(s antlr4512.DITContentRules) (err error) {
return
}

func (r Schema) checkDCOID(s antlr4512.DITContentRule) (err error) {
if soc := r.ObjectClasses().Get(s.OID); soc.IsZero() {
err = mkerr(ErrObjectClassNotFound.Error() + `( superclass: ` + s.OID + `)`)
} else if lup := r.DITContentRules().get(s.OID); !lup.IsZero() {
// fail attempts to marshal a duplicate definition.
err = ErrDuplicateDef
}

return
}

func (r Schema) marshalDC(s antlr4512.DITContentRule) (def DITContentRule, err error) {
if !isNumericOID(s.OID) {
err = ErrMissingNumericOID
Expand All @@ -615,11 +641,10 @@ func (r Schema) marshalDC(s antlr4512.DITContentRule) (def DITContentRule, err e

_def := newDITContentRule()

soc := r.ObjectClasses().Get(s.OID)
if soc.IsZero() {
err = mkerr(ErrObjectClassNotFound.Error() + `( superclass: ` + s.OID + `)`)
if err = r.checkDCOID(s); err != nil {
return
}
soc := r.ObjectClasses().Get(s.OID)

_def.OID = soc
_def.Desc = s.Desc
Expand Down Expand Up @@ -707,6 +732,12 @@ func (r Schema) marshalNF(s antlr4512.NameForm) (def NameForm, err error) {
return
}

// fail attempts to marshal a duplicate definition.
if lup := r.NameForms().get(s.OID); !lup.IsZero() {
err = ErrDuplicateDef
return
}

_def := newNameForm()
_def.OID = s.OID
_def.Desc = s.Desc
Expand Down Expand Up @@ -781,6 +812,12 @@ func (r Schema) marshalDS(s antlr4512.DITStructureRule) (def DITStructureRule, e
return
}

// fail attempts to marshal a duplicate definition.
if lup := r.DITStructureRules().get(ruleid); !lup.IsZero() {
err = ErrDuplicateDef
return
}

_def := newDITStructureRule()
_def.ID = ruleid
_def.Desc = s.Desc
Expand Down
12 changes: 6 additions & 6 deletions schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -555,9 +555,9 @@ func TestLoads_codecov(t *testing.T) {

// supplemental attributeTypes not sourced from an official doc, but
// are useful in UTs, et al.
var suplATs []string = []string{
`( 2.5.18.9 NAME 'hasSubordinates' DESC 'X.501: entry has children' EQUALITY booleanMatch SYNTAX 1.3.6.1.4.1.1466.115.121.1.7 SINGLE-VALUE NO-USER-MODIFICATION USAGE directoryOperation )`,
}
//
// Currently empty.
var suplATs []string = []string{}

func init() {
// Prepare our UT/Example reference schema
Expand Down Expand Up @@ -3213,15 +3213,15 @@ nameform ( 1.3.6.1.4.1.56521.101.2.7.3
#
# 2.8.1. 'rootArcStructure'
#
ditstructurerule ( 0
ditstructurerule ( 20
NAME 'rootArcStructure'
DESC 'structure rule for root arc entries in any dimension; FOR DEMONSTRATION USE ONLY'
FORM nRootArcForm
X-ORIGIN 'draft-coretta-oiddir-schema; unofficial supplement' )
#
# 2.8.2. 'arcStructure'
#
ditstructurerule ( 1
ditstructurerule ( 21
NAME 'arcStructure'
DESC 'structure rule for three dimensional arc entries; FOR DEMONSTRATION USE ONLY'
FORM nArcForm
Expand All @@ -3230,7 +3230,7 @@ ditstructurerule ( 1
#
# 2.8.3 'dotNotArcStructure'
#
ditstructurerule ( 2
ditstructurerule ( 22
NAME 'dotNotArcStructure'
DESC 'structure rule for two dimensional arc entries; FOR DEMONSTRATION USE ONLY'
FORM dotNotationArcForm
Expand Down

0 comments on commit a46937a

Please sign in to comment.