@@ -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,39 @@ 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
+ // UnmarshalFunc allows you to define a custom unmarshaler to parse an Entry values.
309
+ // A custom unmarshaler can be found in the Unmarshal function or in the test files.
310
+ func (e * Entry ) UnmarshalFunc (i interface {},
311
+ fn func (entry * Entry , fieldType reflect.StructField , fieldValue reflect.Value ) error ) error {
312
+ // Make sure it's a ptr
313
+ if vo := reflect .ValueOf (i ).Kind (); vo != reflect .Ptr {
314
+ return fmt .Errorf ("ldap: cannot use %s, expected pointer to a struct" , vo )
315
+ }
316
+
317
+ sv , st := reflect .ValueOf (i ).Elem (), reflect .TypeOf (i ).Elem ()
318
+ // Make sure it's pointing to a struct
319
+ if sv .Kind () != reflect .Struct {
320
+ return fmt .Errorf ("ldap: expected pointer to a struct, got %s" , sv .Kind ())
321
+ }
322
+
323
+ for n := 0 ; n < st .NumField (); n ++ {
324
+ fv , ft := sv .Field (n ), st .Field (n )
325
+
326
+ // skip unexported fields
327
+ if ft .PkgPath != "" {
328
+ continue
329
+ }
330
+
331
+ if err := fn (e , ft , fv ); err != nil {
332
+ return err
333
+ }
323
334
}
324
- return
335
+
336
+ return nil
325
337
}
326
338
327
339
// NewEntryAttribute returns a new EntryAttribute with the desired key-value pair
0 commit comments