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

Commit f30b848

Browse files
Jesse CorettaJesse Coretta
Jesse Coretta
authored and
Jesse Coretta
committed
Added schema.Exists method
1 parent aeb5320 commit f30b848

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

schema.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,37 @@ func (r Schema) Push(def Definition) Schema {
318318
return r
319319
}
320320

321+
/*
322+
Exists returns a Boolean value indicative of whether the receiver instance
323+
contains a matching [Definition] type and identifier.
324+
*/
325+
func (r Schema) Exists(def Definition) (exists bool) {
326+
if def == nil {
327+
return
328+
}
329+
330+
switch def.Type() {
331+
case `ldapSyntax`:
332+
exists = !r.LDAPSyntaxes().Get(def.NumericOID()).IsZero()
333+
case `matchingRule`:
334+
exists = !r.MatchingRules().Get(def.NumericOID()).IsZero()
335+
case `attributeType`:
336+
exists = !r.AttributeTypes().Get(def.NumericOID()).IsZero()
337+
case `matchingRuleUse`:
338+
exists = !r.MatchingRuleUses().Get(def.NumericOID()).IsZero()
339+
case `objectClass`:
340+
exists = !r.ObjectClasses().Get(def.NumericOID()).IsZero()
341+
case `dITContentRule`:
342+
exists = !r.DITContentRules().Get(def.NumericOID()).IsZero()
343+
case `nameForm`:
344+
exists = !r.NameForms().Get(def.NumericOID()).IsZero()
345+
case `dITStructureRule`:
346+
exists = !r.DITStructureRules().Get(def.(DITStructureRule).RuleID()).IsZero()
347+
}
348+
349+
return
350+
}
351+
321352
/*
322353
ParseRaw returns an error following an attempt to parse raw into
323354
usable schema definitions. This method operates similarly to the

schema_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ func ExampleSchema_Push() {
6666
// Output: exampleClass
6767
}
6868

69+
func ExampleSchema_Exists() {
70+
var def Definition = mySchema.LDAPSyntaxes().Get(`integer`)
71+
fmt.Println(mySchema.Exists(def))
72+
// Output: true
73+
}
74+
6975
func ExampleSchema_Replace_objectClass() {
7076

7177
gon := mySchema.ObjectClasses().Get(`groupOfNames`)
@@ -203,6 +209,7 @@ func TestSchema_codecov(t *testing.T) {
203209
mySchema.DITStructureRules().Index(0),
204210
} {
205211
mySchema.Replace(def)
212+
mySchema.Exists(def)
206213
}
207214

208215
mySchema.Replace(LDAPSyntax{})

0 commit comments

Comments
 (0)