Skip to content

Commit 83b05cd

Browse files
4xocxoc
and
xoc
authored
added int and []byte type to entry unmarshal (#382)
Co-authored-by: xoc <[email protected]>
1 parent 05f81f7 commit 83b05cd

File tree

4 files changed

+112
-20
lines changed

4 files changed

+112
-20
lines changed

search.go

+24-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"reflect"
77
"sort"
8+
"strconv"
89
"strings"
910

1011
ber "github.com/go-asn1-ber/asn1-ber"
@@ -184,9 +185,9 @@ func readTag(f reflect.StructField) (string, bool) {
184185
// Unmarshal parses the Entry in the value pointed to by i
185186
//
186187
// Currently, this methods only supports struct fields of type
187-
// string or []string. Other field types will not be regarded.
188-
// If the field type is a string but multiple attribute values
189-
// are returned, the first value will be used to fill the field.
188+
// string, []string, int, int64 or []byte. Other field types will not be
189+
// regarded. If the field type is a string or int but multiple attribute
190+
// values are returned, the first value will be used to fill the field.
190191
//
191192
// Example:
192193
// type UserEntry struct {
@@ -203,6 +204,17 @@ func readTag(f reflect.StructField) (string, bool) {
203204
// // know the amount of attribute values at runtime, use a string array.
204205
// MemberOf []string `ldap:"memberOf"`
205206
//
207+
// // ID is an integer value, it will fail unmarshaling when the given
208+
// // attribute value cannot be parsed into an integer.
209+
// ID int `ldap:"id"`
210+
//
211+
// // LongID is similar to ID but uses an int64 instead.
212+
// LongID int64 `ldap:"longId"`
213+
//
214+
// // Data is similar to MemberOf a slice containing all attribute
215+
// // values.
216+
// Data []byte `ldap:"data"`
217+
//
206218
// // This won't work, as the field is not of type string. For this
207219
// // to work, you'll have to temporarily store the result in string
208220
// // (or string array) and convert it to the desired type afterwards.
@@ -254,8 +266,16 @@ func (e *Entry) Unmarshal(i interface{}) (err error) {
254266
}
255267
case string:
256268
fv.SetString(values[0])
269+
case []byte:
270+
fv.SetBytes([]byte(values[0]))
271+
case int, int64:
272+
intVal, err := strconv.ParseInt(values[0], 10, 64)
273+
if err != nil {
274+
return fmt.Errorf("ldap: could not parse value '%s' into int field", values[0])
275+
}
276+
fv.SetInt(intVal)
257277
default:
258-
return fmt.Errorf("ldap: expected field to be of type string or []string, got %v", ft.Type)
278+
return fmt.Errorf("ldap: expected field to be of type string, []string, int, int64 or []byte, got %v", ft.Type)
259279
}
260280
}
261281
return

search_test.go

+32-6
Original file line numberDiff line numberDiff line change
@@ -86,19 +86,45 @@ func TestEntry_Unmarshal(t *testing.T) {
8686
Values: []string{"[email protected]"},
8787
ByteValues: nil,
8888
},
89+
// Tests int value.
90+
{
91+
Name: "id",
92+
Values: []string{"2147483647"},
93+
ByteValues: nil,
94+
},
95+
// Tests int64 value.
96+
{
97+
Name: "longId",
98+
Values: []string{"9223372036854775807"},
99+
ByteValues: nil,
100+
},
101+
// Tests []byte value.
102+
{
103+
Name: "data",
104+
Values: []string{"data"},
105+
ByteValues: [][]byte{
106+
[]byte("data"),
107+
},
108+
},
89109
},
90110
}
91111

92112
type User struct {
93-
Dn string `ldap:"dn"`
94-
Cn string `ldap:"cn"`
95-
Mail string `ldap:"mail"`
113+
Dn string `ldap:"dn"`
114+
Cn string `ldap:"cn"`
115+
Mail string `ldap:"mail"`
116+
ID int `ldap:"id"`
117+
LongID int64 `ldap:"longId"`
118+
Data []byte `ldap:"data"`
96119
}
97120

98121
expect := &User{
99-
Dn: "cn=mario,ou=Users,dc=go-ldap,dc=github,dc=com",
100-
Cn: "mario",
101-
122+
Dn: "cn=mario,ou=Users,dc=go-ldap,dc=github,dc=com",
123+
Cn: "mario",
124+
125+
ID: 2147483647,
126+
LongID: 9223372036854775807,
127+
Data: []byte("data"),
102128
}
103129
result := &User{}
104130
err := entry.Unmarshal(result)

v3/search.go

+24-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"reflect"
77
"sort"
8+
"strconv"
89
"strings"
910

1011
ber "github.com/go-asn1-ber/asn1-ber"
@@ -184,9 +185,9 @@ func readTag(f reflect.StructField) (string, bool) {
184185
// Unmarshal parses the Entry in the value pointed to by i
185186
//
186187
// Currently, this methods only supports struct fields of type
187-
// string or []string. Other field types will not be regarded.
188-
// If the field type is a string but multiple attribute values
189-
// are returned, the first value will be used to fill the field.
188+
// string, []string, int, int64 or []byte. Other field types will not be
189+
// regarded. If the field type is a string or int but multiple attribute
190+
// values are returned, the first value will be used to fill the field.
190191
//
191192
// Example:
192193
// type UserEntry struct {
@@ -203,6 +204,17 @@ func readTag(f reflect.StructField) (string, bool) {
203204
// // know the amount of attribute values at runtime, use a string array.
204205
// MemberOf []string `ldap:"memberOf"`
205206
//
207+
// // ID is an integer value, it will fail unmarshaling when the given
208+
// // attribute value cannot be parsed into an integer.
209+
// ID int `ldap:"id"`
210+
//
211+
// // LongID is similar to ID but uses an int64 instead.
212+
// LongID int64 `ldap:"longId"`
213+
//
214+
// // Data is similar to MemberOf a slice containing all attribute
215+
// // values.
216+
// Data []byte `ldap:"data"`
217+
//
206218
// // This won't work, as the field is not of type string. For this
207219
// // to work, you'll have to temporarily store the result in string
208220
// // (or string array) and convert it to the desired type afterwards.
@@ -254,8 +266,16 @@ func (e *Entry) Unmarshal(i interface{}) (err error) {
254266
}
255267
case string:
256268
fv.SetString(values[0])
269+
case []byte:
270+
fv.SetBytes([]byte(values[0]))
271+
case int, int64:
272+
intVal, err := strconv.ParseInt(values[0], 10, 64)
273+
if err != nil {
274+
return fmt.Errorf("ldap: could not parse value '%s' into int field", values[0])
275+
}
276+
fv.SetInt(intVal)
257277
default:
258-
return fmt.Errorf("ldap: expected field to be of type string or []string, got %v", ft.Type)
278+
return fmt.Errorf("ldap: expected field to be of type string, []string, int, int64 or []byte, got %v", ft.Type)
259279
}
260280
}
261281
return

v3/search_test.go

+32-6
Original file line numberDiff line numberDiff line change
@@ -86,19 +86,45 @@ func TestEntry_Unmarshal(t *testing.T) {
8686
Values: []string{"[email protected]"},
8787
ByteValues: nil,
8888
},
89+
// Tests int value.
90+
{
91+
Name: "id",
92+
Values: []string{"2147483647"},
93+
ByteValues: nil,
94+
},
95+
// Tests int64 value.
96+
{
97+
Name: "longId",
98+
Values: []string{"9223372036854775807"},
99+
ByteValues: nil,
100+
},
101+
// Tests []byte value.
102+
{
103+
Name: "data",
104+
Values: []string{"data"},
105+
ByteValues: [][]byte{
106+
[]byte("data"),
107+
},
108+
},
89109
},
90110
}
91111

92112
type User struct {
93-
Dn string `ldap:"dn"`
94-
Cn string `ldap:"cn"`
95-
Mail string `ldap:"mail"`
113+
Dn string `ldap:"dn"`
114+
Cn string `ldap:"cn"`
115+
Mail string `ldap:"mail"`
116+
ID int `ldap:"id"`
117+
LongID int64 `ldap:"longId"`
118+
Data []byte `ldap:"data"`
96119
}
97120

98121
expect := &User{
99-
Dn: "cn=mario,ou=Users,dc=go-ldap,dc=github,dc=com",
100-
Cn: "mario",
101-
122+
Dn: "cn=mario,ou=Users,dc=go-ldap,dc=github,dc=com",
123+
Cn: "mario",
124+
125+
ID: 2147483647,
126+
LongID: 9223372036854775807,
127+
Data: []byte("data"),
102128
}
103129
result := &User{}
104130
err := entry.Unmarshal(result)

0 commit comments

Comments
 (0)