Skip to content

Commit 26fbb65

Browse files
committed
Manage multi values (references #596)
1 parent 7a556e9 commit 26fbb65

File tree

1 file changed

+50
-14
lines changed

1 file changed

+50
-14
lines changed

file2ldif.pl

Lines changed: 50 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333
# CSV delimiter (default is ",")
3434
my $csv_delimiter = ";";
3535

36+
# CSV multi values delimiter
37+
my $csv_multivalues_delimiter = ",";
38+
3639
# Strip CSV headers (jump to second line)
3740
my $csv_strip_headers = 1;
3841

@@ -136,7 +139,9 @@
136139
# Write every column as attribute
137140
my $entry = Net::LDAP::Entry->new('o=fakedn');
138141
for my $i ( 0 .. $#columns ) {
139-
$entry->add( $i => $columns[$i] );
142+
my @values =
143+
split( /$csv_multivalues_delimiter/, $columns[$i] );
144+
$entry->add( $i => \@values );
140145
}
141146
$inldif->write_entry($entry);
142147
}
@@ -175,20 +180,21 @@
175180
while ( my ( $k, $v ) = each %localmap ) {
176181
if ( ref($v) eq "ARRAY" ) {
177182
my @values = @$v;
183+
my @all_values;
178184
foreach (@values) {
179-
$_ =~
180-
s/$beginc([^$endc]*)?$endc/&replace_value($entry,$1)/ge;
185+
my $new_values = &generate_value( $entry, $_ );
186+
push @all_values, @$new_values if $new_values;
181187
}
182-
$v = \@values;
188+
$v = \@all_values;
183189
}
184190
else {
185-
$v =~ s/$beginc([^$endc]*)?$endc/&replace_value($entry,$1)/ge;
191+
$v = &generate_value( $entry, $v );
186192
}
187193
$localmap{$k} = $v;
188194
}
189195

190196
# DN
191-
my $dn = $localmap{dn};
197+
my $dn = shift @{ $localmap{'dn'} };
192198
delete $localmap{dn};
193199

194200
# Change operation
@@ -212,9 +218,10 @@
212218
}
213219
}
214220

215-
# Takes the first value of wanted attribute
221+
# Takes all values of wanted attribute
216222
# Removes the whitespaces from begin and end
217223
# Apply subroutine if any
224+
# @return ARRAYREF of values
218225
sub replace_value {
219226
my $entry = shift;
220227
my $key = shift;
@@ -230,23 +237,52 @@ sub replace_value {
230237
else { $attr = $key }
231238

232239
# Replace DN
233-
if ( $attr eq "dn" ) { $value = $entry->dn(); }
240+
if ( $attr eq "dn" ) { $value = [ $entry->dn() ]; }
234241

235-
# Get first attribute value
236-
else { $value = $entry->get_value($attr); }
242+
# Get all attribute values
243+
else { $value = $entry->get_value( $attr, asref => 1 ); }
237244

238245
# Empty value
239246
return "" unless defined $value;
240247

241-
# Trim begin and end whitespaces
242-
$value =~ s/^\s+|\s+$//g;
248+
foreach my $val (@$value) {
249+
250+
# Trim begin and end whitespaces
251+
$val =~ s/^\s+|\s+$//g;
243252

244-
# Apply subroutine if any
245-
$value = &apply_sub( $value, $sub ) if ($sub);
253+
# Apply subroutine if any
254+
$val = &apply_sub( $val, $sub ) if ($sub);
255+
}
246256

247257
return $value;
248258
}
249259

260+
# Create the new values
261+
# Call replace_value to get the mapping
262+
# @return ARRAYREF of new values
263+
sub generate_value {
264+
my $entry = shift;
265+
my $value = shift;
266+
my $key;
267+
my @result;
268+
269+
if ( ($key) = ( $value =~ m/$beginc([^$endc]*)?$endc/ ) ) {
270+
my $new_values = &replace_value( $entry, $key );
271+
if ($new_values) {
272+
foreach my $new_value (@$new_values) {
273+
my $safe_value = $value;
274+
$safe_value =~ s/$beginc([^$endc]*)?$endc/$new_value/ge;
275+
push @result, $safe_value;
276+
}
277+
}
278+
}
279+
else {
280+
push @result, $value;
281+
}
282+
283+
return \@result;
284+
}
285+
250286
# Apply subroutine
251287
sub apply_sub {
252288
my $value = shift;

0 commit comments

Comments
 (0)