Skip to content

Commit cdbbc30

Browse files
authored
feat: extend SearchResult Unmarshal to support *string as field type (#475) (#476)
1 parent f021281 commit cdbbc30

File tree

4 files changed

+26
-6
lines changed

4 files changed

+26
-6
lines changed

search.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,8 @@ func readTag(f reflect.StructField) (string, bool) {
187187
// Unmarshal parses the Entry in the value pointed to by i
188188
//
189189
// Currently, this methods only supports struct fields of type
190-
// string, []string, int, int64, []byte, *DN, []*DN or time.Time. Other field types
191-
// will not be regarded. If the field type is a string or int but multiple
190+
// string, *string, []string, int, int64, []byte, *DN, []*DN or time.Time.
191+
// Other field types will not be regarded. If the field type is a string or int but multiple
192192
// attribute values are returned, the first value will be used to fill the field.
193193
//
194194
// Example:
@@ -279,6 +279,8 @@ func (e *Entry) Unmarshal(i interface{}) (err error) {
279279
}
280280
case string:
281281
fv.SetString(values[0])
282+
case *string:
283+
fv.Set(reflect.ValueOf(&values[0]))
282284
case []byte:
283285
fv.SetBytes([]byte(values[0]))
284286
case int, int64:
@@ -308,7 +310,7 @@ func (e *Entry) Unmarshal(i interface{}) (err error) {
308310
fv.Set(reflect.Append(fv, reflect.ValueOf(dn)))
309311
}
310312
default:
311-
return fmt.Errorf("ldap: expected field to be of type string, []string, int, int64, []byte, *DN, []*DN or time.Time, got %v", ft.Type)
313+
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)
312314
}
313315
}
314316
return

search_test.go

+8
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ func TestEntry_Unmarshal(t *testing.T) {
8787
Values: []string{"mario@go-ldap.com"},
8888
ByteValues: nil,
8989
},
90+
{
91+
Name: "upn",
92+
Values: []string{"mario@go-ldap.com.domain"},
93+
ByteValues: nil,
94+
},
9095
// Tests int value.
9196
{
9297
Name: "id",
@@ -132,6 +137,7 @@ func TestEntry_Unmarshal(t *testing.T) {
132137
Dn string `ldap:"dn"`
133138
Cn string `ldap:"cn"`
134139
Mail string `ldap:"mail"`
140+
UPN *string `ldap:"upn"`
135141
ID int `ldap:"id"`
136142
LongID int64 `ldap:"longId"`
137143
Data []byte `ldap:"data"`
@@ -157,10 +163,12 @@ func TestEntry_Unmarshal(t *testing.T) {
157163
children = append(children, dn)
158164
}
159165

166+
UPN := "mario@go-ldap.com.domain"
160167
expect := &User{
161168
Dn: "cn=mario,ou=Users,dc=go-ldap,dc=github,dc=com",
162169
Cn: "mario",
163170
Mail: "mario@go-ldap.com",
171+
UPN: &UPN,
164172
ID: 2147483647,
165173
LongID: 9223372036854775807,
166174
Data: []byte("data"),

v3/search.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,8 @@ func readTag(f reflect.StructField) (string, bool) {
187187
// Unmarshal parses the Entry in the value pointed to by i
188188
//
189189
// Currently, this methods only supports struct fields of type
190-
// string, []string, int, int64, []byte, *DN, []*DN or time.Time. Other field types
191-
// will not be regarded. If the field type is a string or int but multiple
190+
// string, *string, []string, int, int64, []byte, *DN, []*DN or time.Time.
191+
// Other field types will not be regarded. If the field type is a string or int but multiple
192192
// attribute values are returned, the first value will be used to fill the field.
193193
//
194194
// Example:
@@ -279,6 +279,8 @@ func (e *Entry) Unmarshal(i interface{}) (err error) {
279279
}
280280
case string:
281281
fv.SetString(values[0])
282+
case *string:
283+
fv.Set(reflect.ValueOf(&values[0]))
282284
case []byte:
283285
fv.SetBytes([]byte(values[0]))
284286
case int, int64:
@@ -308,7 +310,7 @@ func (e *Entry) Unmarshal(i interface{}) (err error) {
308310
fv.Set(reflect.Append(fv, reflect.ValueOf(dn)))
309311
}
310312
default:
311-
return fmt.Errorf("ldap: expected field to be of type string, []string, int, int64, []byte, *DN, []*DN or time.Time, got %v", ft.Type)
313+
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)
312314
}
313315
}
314316
return

v3/search_test.go

+8
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ func TestEntry_Unmarshal(t *testing.T) {
8787
Values: []string{"mario@go-ldap.com"},
8888
ByteValues: nil,
8989
},
90+
{
91+
Name: "upn",
92+
Values: []string{"mario@go-ldap.com.domain"},
93+
ByteValues: nil,
94+
},
9095
// Tests int value.
9196
{
9297
Name: "id",
@@ -132,6 +137,7 @@ func TestEntry_Unmarshal(t *testing.T) {
132137
Dn string `ldap:"dn"`
133138
Cn string `ldap:"cn"`
134139
Mail string `ldap:"mail"`
140+
UPN *string `ldap:"upn"`
135141
ID int `ldap:"id"`
136142
LongID int64 `ldap:"longId"`
137143
Data []byte `ldap:"data"`
@@ -157,10 +163,12 @@ func TestEntry_Unmarshal(t *testing.T) {
157163
children = append(children, dn)
158164
}
159165

166+
UPN := "mario@go-ldap.com.domain"
160167
expect := &User{
161168
Dn: "cn=mario,ou=Users,dc=go-ldap,dc=github,dc=com",
162169
Cn: "mario",
163170
Mail: "mario@go-ldap.com",
171+
UPN: &UPN,
164172
ID: 2147483647,
165173
LongID: 9223372036854775807,
166174
Data: []byte("data"),

0 commit comments

Comments
 (0)