question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

LdapEntry Handle Missing Keys

See original GitHub issue

If 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:open
  • Created 3 years ago
  • Reactions:2
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
nesc58commented, Feb 26, 2021

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 calling GetAttributeSet()....

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:

public string GetAttributeValueOrDefault([NotNull] string attributeName, string fallback)
{
    if (!Attrs.TryGetValue(attributeName, out var attribute)) return fallback;
    return attribute.StringValue ?? fallback;
}

The call would looks like ldapEntry.GetAttributeValueOrDefault(TestAttributeName, "111-222-3333")

Overloads must exists for all types: string, string[], byte[] and byte[][] as well to have a consistent api.

For all otherr cases we could provide a function which returns a default attribute as well.

public LdapAttribute GetOrDefault([NotNull] string attributeName, LdapAttribute fallback = default)
{
    if (!Attrs.TryGetValue(attributeName, out var attribute)) return fallback;
    return attribute;
}

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.

1reaction
photoacrosscommented, Oct 5, 2022

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found