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

Commit

Permalink
Added schema.Exists method
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 aeb5320 commit f30b848
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
31 changes: 31 additions & 0 deletions schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,37 @@ func (r Schema) Push(def Definition) Schema {
return r
}

/*
Exists returns a Boolean value indicative of whether the receiver instance
contains a matching [Definition] type and identifier.
*/
func (r Schema) Exists(def Definition) (exists bool) {
if def == nil {
return
}

switch def.Type() {
case `ldapSyntax`:
exists = !r.LDAPSyntaxes().Get(def.NumericOID()).IsZero()
case `matchingRule`:
exists = !r.MatchingRules().Get(def.NumericOID()).IsZero()
case `attributeType`:
exists = !r.AttributeTypes().Get(def.NumericOID()).IsZero()
case `matchingRuleUse`:
exists = !r.MatchingRuleUses().Get(def.NumericOID()).IsZero()
case `objectClass`:
exists = !r.ObjectClasses().Get(def.NumericOID()).IsZero()
case `dITContentRule`:
exists = !r.DITContentRules().Get(def.NumericOID()).IsZero()
case `nameForm`:
exists = !r.NameForms().Get(def.NumericOID()).IsZero()
case `dITStructureRule`:
exists = !r.DITStructureRules().Get(def.(DITStructureRule).RuleID()).IsZero()
}

return
}

/*
ParseRaw returns an error following an attempt to parse raw into
usable schema definitions. This method operates similarly to the
Expand Down
7 changes: 7 additions & 0 deletions schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ func ExampleSchema_Push() {
// Output: exampleClass
}

func ExampleSchema_Exists() {
var def Definition = mySchema.LDAPSyntaxes().Get(`integer`)
fmt.Println(mySchema.Exists(def))
// Output: true
}

func ExampleSchema_Replace_objectClass() {

gon := mySchema.ObjectClasses().Get(`groupOfNames`)
Expand Down Expand Up @@ -203,6 +209,7 @@ func TestSchema_codecov(t *testing.T) {
mySchema.DITStructureRules().Index(0),
} {
mySchema.Replace(def)
mySchema.Exists(def)
}

mySchema.Replace(LDAPSyntax{})
Expand Down

0 comments on commit f30b848

Please sign in to comment.