@@ -246,38 +246,19 @@ func readTag(f reflect.StructField) (string, bool) {
246
246
// // ...
247
247
// }
248
248
func (e * Entry ) Unmarshal (i interface {}) (err error ) {
249
- // Make sure it's a ptr
250
- if vo := reflect .ValueOf (i ).Kind (); vo != reflect .Ptr {
251
- return fmt .Errorf ("ldap: cannot use %s, expected pointer to a struct" , vo )
252
- }
253
-
254
- sv , st := reflect .ValueOf (i ).Elem (), reflect .TypeOf (i ).Elem ()
255
- // Make sure it's pointing to a struct
256
- if sv .Kind () != reflect .Struct {
257
- return fmt .Errorf ("ldap: expected pointer to a struct, got %s" , sv .Kind ())
258
- }
259
-
260
- for n := 0 ; n < st .NumField (); n ++ {
261
- // Holds struct field value and type
262
- fv , ft := sv .Field (n ), st .Field (n )
263
-
264
- // skip unexported fields
265
- if ft .PkgPath != "" {
266
- continue
267
- }
268
-
249
+ return e .UnmarshalFunc (i , func (entry * Entry , ft reflect.StructField , fv reflect.Value ) error {
269
250
// omitempty can be safely discarded, as it's not needed when unmarshalling
270
251
fieldTag , _ := readTag (ft )
271
252
272
253
// Fill the field with the distinguishedName if the tag key is `dn`
273
254
if fieldTag == "dn" {
274
255
fv .SetString (e .DN )
275
- continue
256
+ return nil
276
257
}
277
258
278
259
values := e .GetAttributeValues (fieldTag )
279
260
if len (values ) == 0 {
280
- continue
261
+ return nil
281
262
}
282
263
283
264
switch fv .Interface ().(type ) {
@@ -320,8 +301,37 @@ func (e *Entry) Unmarshal(i interface{}) (err error) {
320
301
default :
321
302
return fmt .Errorf ("ldap: expected field to be of type string, *string, []string, int, int64, []byte, *DN, []*DN or time.Time, got %v" , ft .Type )
322
303
}
304
+ return nil
305
+ })
306
+ }
307
+
308
+ func (e * Entry ) UnmarshalFunc (i interface {},
309
+ fn func (entry * Entry , fieldType reflect.StructField , fieldValue reflect.Value ) error ) error {
310
+ // Make sure it's a ptr
311
+ if vo := reflect .ValueOf (i ).Kind (); vo != reflect .Ptr {
312
+ return fmt .Errorf ("ldap: cannot use %s, expected pointer to a struct" , vo )
313
+ }
314
+
315
+ sv , st := reflect .ValueOf (i ).Elem (), reflect .TypeOf (i ).Elem ()
316
+ // Make sure it's pointing to a struct
317
+ if sv .Kind () != reflect .Struct {
318
+ return fmt .Errorf ("ldap: expected pointer to a struct, got %s" , sv .Kind ())
319
+ }
320
+
321
+ for n := 0 ; n < st .NumField (); n ++ {
322
+ fv , ft := sv .Field (n ), st .Field (n )
323
+
324
+ // skip unexported fields
325
+ if ft .PkgPath != "" {
326
+ continue
327
+ }
328
+
329
+ if err := fn (e , ft , fv ); err != nil {
330
+ return err
331
+ }
323
332
}
324
- return
333
+
334
+ return nil
325
335
}
326
336
327
337
// NewEntryAttribute returns a new EntryAttribute with the desired key-value pair
0 commit comments