[prev in list] [next in list] [prev in thread] [next in thread] 

List:       perl-ldap-dev
Subject:    Dynamic method for determing single or multi-value attributes
From:       Dan Cutler <DCutler () intelimedix ! com>
Date:       2013-10-11 20:38:56
Message-ID: F9EBEB8DE1C1184C9BB2BD64BD33A40809EAB1 () DSM-Mail01 ! dsm ! net
[Download RAW message or body]

Fellow LDAPers,

I have dozens of Net::LDAP, Entry, LDIF, Schema etc based scripts (Open Source \
rocks!).

However, I keep finding myself in the same situation wishing I had a more dynamic \
approach towards handling multi-valued attributes.

I suppose I could ALWAYS use array references even for attrs that I know are single \
valued: ie "$rarray = $entr->get_value->($some_specific_attr, asref => 1)". Where the \
"{asref => 1}" option forces an array ref for the returned value even if its only \
single valued.

But then I really don't want to deal with $$rarray[0], or if (scalar @$array == 1) \
etc??

A little bit poking around the Net::LDAP::Schema seems to indicate that
  if (! exists $schema->attribute->('some_specifc_attr')->{single-value}) {
     # it's multi-valued - ie; the 'single-value' is set to '1' but I don't see any \
set to zero, rather the key just doesn't exist if its multi-valued.  else
   #its single valued.

But the schema is really big and I'd have to somehow cache/stash the small parts of \
this monster that I'd need throughout the lifetime of the script;-)

Today I typically have static constants files that are like:

our @multi_value_attrs = qw[mail, memberof, etc] and check this every time I fetch or \
process attributes.

if (some_specific_attr in @multi_value_attrs) {
   @answers = $entr->get_value('some_specific_attr'); #array
} else {
  $answer = $entr->get_value('some_specific_attr');  #scalar
}

Its very inelegant...

I see many posts out there explaining the whole "fetch multi-valued attributes as an \
array and single valued attributes as scalars" but no simple way of knowing \
dynamically which to expect.

Does anyone have an inexpensive function like:
$entr->is_multi_valued('some_specific_attr')?

Or maybe it's just my approach that is flawed?

Thanks all!
--Dan


[Attachment #3 (text/html)]

<html xmlns:v="urn:schemas-microsoft-com:vml" \
xmlns:o="urn:schemas-microsoft-com:office:office" \
xmlns:w="urn:schemas-microsoft-com:office:word" \
xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" \
xmlns="http://www.w3.org/TR/REC-html40"> <head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<meta name="Generator" content="Microsoft Word 14 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
	{font-family:Calibri;
	panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
	{margin:0in;
	margin-bottom:.0001pt;
	font-size:11.0pt;
	font-family:"Calibri","sans-serif";}
a:link, span.MsoHyperlink
	{mso-style-priority:99;
	color:blue;
	text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
	{mso-style-priority:99;
	color:purple;
	text-decoration:underline;}
span.EmailStyle17
	{mso-style-type:personal-compose;
	font-family:"Calibri","sans-serif";
	color:windowtext;}
.MsoChpDefault
	{mso-style-type:export-only;
	font-family:"Calibri","sans-serif";}
@page WordSection1
	{size:8.5in 11.0in;
	margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
	{page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-US" link="blue" vlink="purple">
<div class="WordSection1">
<p class="MsoNormal">Fellow LDAPers,<o:p></o:p></p>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
<p class="MsoNormal">I have dozens of Net::LDAP, Entry, LDIF, Schema etc based \
scripts (Open Source rocks!).<o:p></o:p></p> <p \
class="MsoNormal"><o:p>&nbsp;</o:p></p> <p class="MsoNormal">However, I keep finding \
myself in the same situation wishing I had a more dynamic approach towards handling \
multi-valued attributes.<o:p></o:p></p> <p class="MsoNormal"><o:p>&nbsp;</o:p></p>
<p class="MsoNormal">I suppose I could ALWAYS use array references even for attrs \
that I know are single valued: ie &#8220;$rarray = \
$entr-&gt;get_value-&gt;($some_specific_attr, <b><span style="color:red">asref =&gt; \
1</span></b>)&#8221;.<o:p></o:p></p> <p class="MsoNormal">Where the &#8220;{asref \
=&gt; 1}&#8221; option forces an array ref for the returned value even if its only \
single valued.<o:p></o:p></p> <p class="MsoNormal"><o:p>&nbsp;</o:p></p>
<p class="MsoNormal">But then I really don&#8217;t want to deal with $$rarray[0], or \
if (scalar @$array == 1) etc??<o:p></o:p></p> <p \
class="MsoNormal"><o:p>&nbsp;</o:p></p> <p class="MsoNormal">A little bit poking \
around the Net::LDAP::Schema seems to indicate that <o:p></o:p></p>
<p class="MsoNormal">&nbsp;&nbsp;if (! exists \
$schema-&gt;attribute-&gt;(&#8216;some_specifc_attr&#8217;)-&gt;{single-value}) \
{<o:p></o:p></p> <p class="MsoNormal">&nbsp;&nbsp;&nbsp;&nbsp; # it&#8217;s \
multi-valued &#8211; ie; the &#8216;single-value&#8217; is set to &#8216;1&#8217; but \
I don&#8217;t see any set to zero, rather the key just doesn&#8217;t exist if its \
multi-valued.<o:p></o:p></p> <p class="MsoNormal">&nbsp; else<o:p></o:p></p>
<p class="MsoNormal">&nbsp;&nbsp; #its single valued.<o:p></o:p></p>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
<p class="MsoNormal">But the schema is really big and I&#8217;d have to somehow \
cache/stash the small parts of this monster that I&#8217;d need throughout the \
lifetime of the script;-)<o:p></o:p></p> <p class="MsoNormal"><o:p>&nbsp;</o:p></p>
<p class="MsoNormal">Today I typically have static constants files that are \
like:<o:p></o:p></p> <p class="MsoNormal"><o:p>&nbsp;</o:p></p>
<p class="MsoNormal">our @multi_value_attrs = qw[mail, memberof, etc] and check this \
every time I fetch or process attributes.<o:p></o:p></p> <p \
class="MsoNormal"><o:p>&nbsp;</o:p></p> <p class="MsoNormal">if (some_specific_attr \
in @multi_value_attrs) {<o:p></o:p></p> <p class="MsoNormal">&nbsp;&nbsp; @answers = \
$entr-&gt;get_value(&#8216;some_specific_attr&#8217;); #array<o:p></o:p></p> <p \
class="MsoNormal">} else {<o:p></o:p></p> <p class="MsoNormal">&nbsp; $answer = \
$entr-&gt;get_value(&#8216;some_specific_attr&#8217;);&nbsp; #scalar<o:p></o:p></p> \
<p class="MsoNormal">}<o:p></o:p></p> <p class="MsoNormal"><o:p>&nbsp;</o:p></p>
<p class="MsoNormal">Its very inelegant&#8230;<o:p></o:p></p>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
<p class="MsoNormal">I see many posts out there explaining the whole &#8220;fetch \
multi-valued attributes as an array and single valued attributes as scalars&#8221; \
but no simple way of knowing dynamically which to expect.<o:p></o:p></p> <p \
class="MsoNormal"><o:p>&nbsp;</o:p></p> <p class="MsoNormal">Does anyone have an \
inexpensive function like:<o:p></o:p></p> <p \
class="MsoNormal">$entr-&gt;is_multi_valued(&#8216;some_specific_attr&#8217;)?<o:p></o:p></p>
 <p class="MsoNormal"><o:p>&nbsp;</o:p></p>
<p class="MsoNormal">Or maybe it&#8217;s just my approach that is \
flawed?<o:p></o:p></p> <p class="MsoNormal"><o:p>&nbsp;</o:p></p>
<p class="MsoNormal">Thanks all!<o:p></o:p></p>
<p class="MsoNormal">--Dan<o:p></o:p></p>
</div>
</body>
</html>



[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic