LdapEntry Handle Missing Keys
See original GitHub issueIf you are fetching values from an LdapEntry
, and want to make sure they key exists (if the key does not exist, you get an exception), you have to do the following:
entry.GetAttributeSet().ContainsKey('TelephoneNumber') ? entry.GetAttribute('TelephoneNumber').StringValue : "111-222-3333"
While this is not terrible, it seems a little odd that LdapEntry
does not either:
a) Properly return null if the key does not exist without throwing an error
b) Implement its own ContainsKey
method that simply calls ContainsKey
on the LdapAttributeSet
Personally I would prefer option a, as you could rewrite the above code as:
entry.GetAttribute('TelephoneNumber')?.StringValue ?? "111-222-3333"
Just much cleaner and has a more expected outcome.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Handling missing keys in Python dictionaries
... aware user that a particular key is absent or to pop a default message in that place, there are several methods to...
Read more >Spring LDAP Reference
Name instances as attribute values are now handled properly with ... the minimum number of object instances per key remain in the pool....
Read more >salt.states.ldap
The states.ldap state module allows you to manage LDAP entries and their ... if it is missing but you do want to preserve...
Read more >Handle missing key from dict - python
You can use .get(key, defualt_value) to get the value from a dict, or if one is not present it will use the default...
Read more >How To Use LDIF Files to Make Changes to an OpenLDAP ...
LDIF works using a basic key-value system, with one statement per-line. ... OpenLDAP provides tools that can handle both additions and ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
What is about a well known TryGet pattern:
bool TryGet (string attributeName, out LdapAttribute foundAttribute)
?Forget it… The call looks not clean enough. To fallback all null cases the call must look like
ldapEntry.TryGetAttribute(TestAttributeName, out var r) ? r.StringValue ?? "111-222-3333" : "111-222-3333"
. Thats terrible. But it would be fine instead of callingGetAttributeSet()....
What do you think about a more explicit function to returning given default values. Something like
GetOrDefault
on LdapEntry or a extension method. Something like:The call would looks like
ldapEntry.GetAttributeValueOrDefault(TestAttributeName, "111-222-3333")
Overloads must exists for all types:
string
,string[]
,byte[]
andbyte[][]
as well to have a consistent api.For all otherr cases we could provide a function which returns a default attribute as well.
So the existing API would not change and the default behavior in untouched. Returning null is always a bad thing for me but in a special scope it seem legit. For me in a case like GetOrDefault, because the signature already tells me as a developer the value returns in fallback. Instead of null i love e.g. option types, TryGet or GetOrDefault methods.
Many thanks this saved me a bit of a headache - had to modify some VB code which was throwing up an error ‘The given key was not present in the dictionary’ for certain usernames. When comparing LDAP entries, some of these users did not have a particular attribute, hence the above error.
VB code below if anyone finds that useful.
If _ldapUser.GetAttributeSet().ContainsKey("sn") Then sname = _ldapUser.GetAttribute("sn").StringValue Else sname = "Unknown" End If