Skip to content

Commit

Permalink
feat: extend SearchResult Unmarshal to support *string as field type (
Browse files Browse the repository at this point in the history
  • Loading branch information
cpuschma authored and gustavoluvizotto committed Apr 11, 2024
1 parent 707a142 commit 032ab34
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
8 changes: 5 additions & 3 deletions search.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -308,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
Expand Down
8 changes: 8 additions & 0 deletions search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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"`
Expand All @@ -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"),
Expand Down
8 changes: 5 additions & 3 deletions v3/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -308,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
Expand Down
8 changes: 8 additions & 0 deletions v3/search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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"`
Expand All @@ -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"),
Expand Down

0 comments on commit 032ab34

Please sign in to comment.