在我的应用程序中,我将默认配置参数保存在toml配置文件中。在我存储的配置文件中:
LdapUserDefaultObjectClass = '"inetOrgPerson","extensibleObject","posixAccount"'
但是,当我使用下面的代码所示的变量时,用户添加事务失败,并显示:
RESULT tag=105 err=21 text=objectClass: value #0 invalid per syntax
从视觉上看,从 toml 文件传递的参数和键入的字符串是相同的。是否要求对对象进行硬编码?在我见过的几个例子中,没有一个对 objectClass 使用变量,但对其他属性使用变量。
添加用户失败的代码(不起作用(
userAdd := ldap.NewAddRequest("uid=tester007@example,ou=users,dc=example", nil)
userAdd.Attribute("objectClass", []string{ldapClient.LDAP.LdapUserDefaultObjectClas})
// bunch of other attributes that satisfy the object classes below this line
// ...
ldapConn.Add(userAdd)
返回的错误:
RESULT tag=105 err=21 text=objectClass: value #0 invalid per syntax
成功添加用户的代码(有效!
userAdd := ldap.NewAddRequest("uid=tester007@example,ou=users,dc=example", nil)
userAdd.Attribute("objectClass", []string {"inetOrgPerson","extensibleObject","posixAccount"})
/// bunch of other attributes that satisfy the object classes below this line
...
ldapConn.Add(userAdd)
在进一步调试后,我可以看到利用 TOML 变量的代码的解析方式与我对文字进行硬编码时不同。我不完全明白这里发生了什么,以及为什么它们被更改/翻译和逃避。我知道这不是TOML问题,而是将字符串提供给userAdd.Attribute("objectClass",[]string {}(时发生的事情。
我是Go的新手,我有一种感觉,这是一个duh!类型问题,但我尝试以多种方式转换和引用东西,结果总是错误的......
使用 TOML 变量时调试失败的输出
Add Request: (Application, Constructed, 0x08) Len=368 "<nil>"
DN: (Universal, Primitive, Octet String) Len=39 "uid=tester007@example,ou=users,dc=example"
Attributes: (Universal, Constructed, Sequence and Sequence of) Len=323 "<nil>"
Attribute: (Universal, Constructed, Sequence and Sequence of) Len=66 "<nil>"
Type: (Universal, Primitive, Octet String) Len=11 "objectClass"
AttributeValue: (Universal, Constructed, Set and Set OF) Len=51 "<nil>"
Vals: (Universal, Primitive, Octet String) Len=49 ""inetOrgPerson","extensibleObject","posixAccount""
调试输出显示添加成功。这是对值进行硬编码的时候。
Add Request: (Application, Constructed, 0x08) Len=364 "<nil>"
DN: (Universal, Primitive, Octet String) Len=39 "uid=tester007@example,ou=users,dc=example"
Attributes: (Universal, Constructed, Sequence and Sequence of) Len=319 "<nil>"
Attribute: (Universal, Constructed, Sequence and Sequence of) Len=62 "<nil>"
Type: (Universal, Primitive, Octet String) Len=11 "objectClass"
AttributeValue: (Universal, Constructed, Set and Set OF) Len=47 "<nil>"
Vals: (Universal, Primitive, Octet String) Len=13 "inetOrgPerson"
Vals: (Universal, Primitive, Octet String) Len=16 "extensibleObject"
Vals: (Universal, Primitive, Octet String) Len=12 "posixAccount"
我想我想通了(n00b(。
TOML的定义很好。问题在于我如何尝试填充 objectClass 属性。我试图将string
传递到[]string
(字符串结构?我不明白的是,我可以将自己的[]string
传递给函数。
基本上我不明白[]string{}
可以用我自己的[]string
变量替换:
错:
userAdd.Attribute("objectClass", []string{ldapClient.LDAP.LdapUserDefaultObjectClass})
正确:
- assign ldapClient.LDAP.LdapUserDefaultObjectClass to objClass - nice not need
objClass := ldapClient.LDAP.LdapUserDefaultObjectClass
- 将对象类拆分为切片
userObjectClasses := (strings.Split(objClass, ","))
- 使用 userObjectClasses 传递 AttVals。
userAdd.Attribute("objectClass", userObjectClasses)
调试输出:
Add Request: (Application, Constructed, 0x08) Len=364 "<nil>"
DN: (Universal, Primitive, Octet String) Len=39 "uid=tester007@example,ou=users,dc=example"
Attributes: (Universal, Constructed, Sequence and Sequence of) Len=319 "<nil>"
Attribute: (Universal, Constructed, Sequence and Sequence of) Len=62 "<nil>"
Type: (Universal, Primitive, Octet String) Len=11 "objectClass"
AttributeValue: (Universal, Constructed, Set and Set OF) Len=47 "<nil>"
Vals: (Universal, Primitive, Octet String) Len=13 "inetOrgPerson"
Vals: (Universal, Primitive, Octet String) Len=16 "extensibleObject"
Vals: (Universal, Primitive, Octet String) Len=12 "posixAccount"
我希望这可以帮助其他人学习 Go 和 LDAP 包。
- 理智保护程序:https://golang.org/pkg/strings/#Split
- 有帮助: https://medium.com/golangspec/composite-literals-in-go-10dc62eec06a