From 27047653b5e0f2be0d46849e8aa7da3d886c2ce1 Mon Sep 17 00:00:00 2001 From: Christopher Puschmann Date: Mon, 11 Dec 2023 22:02:09 +0100 Subject: [PATCH 1/2] feat: extend SearchResult `Unmarshal` to support *string as field type --- search.go | 2 ++ search_test.go | 8 ++++++++ v3/search.go | 2 ++ v3/search_test.go | 8 ++++++++ 4 files changed, 20 insertions(+) diff --git a/search.go b/search.go index 4eb10762..2cb128d2 100644 --- a/search.go +++ b/search.go @@ -279,6 +279,8 @@ func (e *Entry) Unmarshal(i interface{}) (err error) { } case string: fv.SetString(values[0]) + case *string: + fv.Set(reflect.ValueOf(&values[0])) case []byte: fv.SetBytes([]byte(values[0])) case int, int64: diff --git a/search_test.go b/search_test.go index 2386513d..029029e0 100644 --- a/search_test.go +++ b/search_test.go @@ -87,6 +87,11 @@ func TestEntry_Unmarshal(t *testing.T) { Values: []string{"mario@go-ldap.com"}, ByteValues: nil, }, + { + Name: "upn", + Values: []string{"mario@go-ldap.com.domain"}, + ByteValues: nil, + }, // Tests int value. { Name: "id", @@ -132,6 +137,7 @@ func TestEntry_Unmarshal(t *testing.T) { Dn string `ldap:"dn"` Cn string `ldap:"cn"` Mail string `ldap:"mail"` + UPN *string `ldap:"upn"` ID int `ldap:"id"` LongID int64 `ldap:"longId"` Data []byte `ldap:"data"` @@ -157,10 +163,12 @@ func TestEntry_Unmarshal(t *testing.T) { children = append(children, dn) } + UPN := "mario@go-ldap.com.domain" expect := &User{ Dn: "cn=mario,ou=Users,dc=go-ldap,dc=github,dc=com", Cn: "mario", Mail: "mario@go-ldap.com", + UPN: &UPN, ID: 2147483647, LongID: 9223372036854775807, Data: []byte("data"), diff --git a/v3/search.go b/v3/search.go index 4eb10762..2cb128d2 100644 --- a/v3/search.go +++ b/v3/search.go @@ -279,6 +279,8 @@ func (e *Entry) Unmarshal(i interface{}) (err error) { } case string: fv.SetString(values[0]) + case *string: + fv.Set(reflect.ValueOf(&values[0])) case []byte: fv.SetBytes([]byte(values[0])) case int, int64: diff --git a/v3/search_test.go b/v3/search_test.go index 2386513d..029029e0 100644 --- a/v3/search_test.go +++ b/v3/search_test.go @@ -87,6 +87,11 @@ func TestEntry_Unmarshal(t *testing.T) { Values: []string{"mario@go-ldap.com"}, ByteValues: nil, }, + { + Name: "upn", + Values: []string{"mario@go-ldap.com.domain"}, + ByteValues: nil, + }, // Tests int value. { Name: "id", @@ -132,6 +137,7 @@ func TestEntry_Unmarshal(t *testing.T) { Dn string `ldap:"dn"` Cn string `ldap:"cn"` Mail string `ldap:"mail"` + UPN *string `ldap:"upn"` ID int `ldap:"id"` LongID int64 `ldap:"longId"` Data []byte `ldap:"data"` @@ -157,10 +163,12 @@ func TestEntry_Unmarshal(t *testing.T) { children = append(children, dn) } + UPN := "mario@go-ldap.com.domain" expect := &User{ Dn: "cn=mario,ou=Users,dc=go-ldap,dc=github,dc=com", Cn: "mario", Mail: "mario@go-ldap.com", + UPN: &UPN, ID: 2147483647, LongID: 9223372036854775807, Data: []byte("data"), From 68e92f4792620fa1c7c248f98d4aeec3f48b54f9 Mon Sep 17 00:00:00 2001 From: Christopher Puschmann Date: Mon, 11 Dec 2023 22:06:30 +0100 Subject: [PATCH 2/2] feat: extend SearchResult `Unmarshal` to support *string as field type --- search.go | 6 +++--- v3/search.go | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/search.go b/search.go index 2cb128d2..d95df2f3 100644 --- a/search.go +++ b/search.go @@ -187,8 +187,8 @@ func readTag(f reflect.StructField) (string, bool) { // Unmarshal parses the Entry in the value pointed to by i // // Currently, this methods only supports struct fields of type -// string, []string, int, int64, []byte, *DN, []*DN or time.Time. Other field types -// will not be regarded. If the field type is a string or int but multiple +// string, *string, []string, int, int64, []byte, *DN, []*DN or time.Time. +// Other field types will not be regarded. If the field type is a string or int but multiple // attribute values are returned, the first value will be used to fill the field. // // Example: @@ -310,7 +310,7 @@ func (e *Entry) Unmarshal(i interface{}) (err error) { fv.Set(reflect.Append(fv, reflect.ValueOf(dn))) } default: - return fmt.Errorf("ldap: expected field to be of type string, []string, int, int64, []byte, *DN, []*DN or time.Time, got %v", ft.Type) + 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) } } return diff --git a/v3/search.go b/v3/search.go index 2cb128d2..d95df2f3 100644 --- a/v3/search.go +++ b/v3/search.go @@ -187,8 +187,8 @@ func readTag(f reflect.StructField) (string, bool) { // Unmarshal parses the Entry in the value pointed to by i // // Currently, this methods only supports struct fields of type -// string, []string, int, int64, []byte, *DN, []*DN or time.Time. Other field types -// will not be regarded. If the field type is a string or int but multiple +// string, *string, []string, int, int64, []byte, *DN, []*DN or time.Time. +// Other field types will not be regarded. If the field type is a string or int but multiple // attribute values are returned, the first value will be used to fill the field. // // Example: @@ -310,7 +310,7 @@ func (e *Entry) Unmarshal(i interface{}) (err error) { fv.Set(reflect.Append(fv, reflect.ValueOf(dn))) } default: - return fmt.Errorf("ldap: expected field to be of type string, []string, int, int64, []byte, *DN, []*DN or time.Time, got %v", ft.Type) + 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) } } return