Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: extend SearchResult Unmarshal to support *string as field type (#475) #476

Merged
merged 2 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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