在 Tcl REST 的字典配置数组中使用变量
Using variables in Tcl REST's config array of dicts
如何在 tcllib 的 rest package 的接口定义变量中使用由两个 whitespace-separated 字符串组成的变量?
接口用法指定了一组 REST 调用定义,它们本身就是字典。
在这个特定的 API 案例中,我需要传入从之前的调用中检索到的身份验证令牌。如何在定义配置数组时将该值用作变量?
set token "Token "
append token 123456789ABCD
array set restApi {
callFoo {
url http://example.com/foo
method get
format json
headers {Auth $token}
}
}
rest::create_interface restApi
set resultDict [restApi::callFoo]
headers {Auth $token}
和 headers "Auth $token"
都会导致发送文字 Auth: $token
header。同样,headers {Auth [set token]}
发送 Auth: [set token]
.
数组定义中的变量似乎没有被插入,鉴于大括号的标准行为,这并不完全出乎意料,尽管它们通常用于分组 multi-dimensional 字典。
在字典 (headers Auth "$token"
) 中将引号放在 $token
周围确实会进行插值,但出现以下错误。
missing value to go with key
while executing
"dict for {k val} [dict get $config headers] {
_addopts $val config
}"
(procedure "rest::create_interface" line 36)
invoked from within
"rest::create_interface restApi"
(file "t.tcl" line 86)
最初我认为 $headers 字典中的奇数个单词打破了 dict for
。但是从 $token
中删除空格不会改变错误。
在配置数组之外设置值会导致相同的错误:
dict set restApi(callFoo) headers "Auth $token"
我很茫然。请帮助我,Obi-Wan Kenobi。你是我唯一的希望!
2019 年 11 月 30 日更新
事实证明,该实现包括通过用 %
包装变量名称来将变量嵌入到配置字典中的能力。变量的值可以设置为函数的选项:
array set restApi {
callFoo {
url http://example.com/foo
method get
format json
headers "Auth %tokenVar%"
}
}
rest::create_interface restApi
restApi::callFoo -tokenVar $token
还应注意,包允许在调用之前设置这些变量:
restApi::set_static_args tokenVar $token
restApi::callFoo
以这种方式设置的变量适用于所有定义的 REST 调用。因此,如果多个调用需要 Auth
header,只需将 headers "Auth %tokenVar%
添加到每个调用的配置中即可。
以前的答案,为后代保存
感谢 Andreas,他找到了答案。使用以下语法设置 headers 字典有效:
array set restApi {
callFoo {
url http://example.com/foo
method get
format json
}
}
dict set restApi(callFoo) headers [list Auth $token]
这也有效:
array set restApi {
callFoo {
url http://example.com/foo
method get
format json
headers [list Auth $token]
}
}
set restApi(callFoo) [subst $restApi(callFoo)]
后一种形式可能更灵活,因为可以在数组定义中使用多个变量,并且在调用 rest::create_interface
之前,遍历每个元素并执行 subst
.
为了后代,我会注意到我可能曾经尝试过第一种方法,但是因为我有 "commented out" 一些在定义 headers dict 的尝试失败了restApi 数组定义,它 "broke" 数组。那是因为解析器没有将 #headers
line 解释为注释,而是 string #headers
被解释作为字典键。
如何在 tcllib 的 rest package 的接口定义变量中使用由两个 whitespace-separated 字符串组成的变量?
接口用法指定了一组 REST 调用定义,它们本身就是字典。
在这个特定的 API 案例中,我需要传入从之前的调用中检索到的身份验证令牌。如何在定义配置数组时将该值用作变量?
set token "Token "
append token 123456789ABCD
array set restApi {
callFoo {
url http://example.com/foo
method get
format json
headers {Auth $token}
}
}
rest::create_interface restApi
set resultDict [restApi::callFoo]
headers {Auth $token}
和 headers "Auth $token"
都会导致发送文字 Auth: $token
header。同样,headers {Auth [set token]}
发送 Auth: [set token]
.
数组定义中的变量似乎没有被插入,鉴于大括号的标准行为,这并不完全出乎意料,尽管它们通常用于分组 multi-dimensional 字典。
在字典 (headers Auth "$token"
) 中将引号放在 $token
周围确实会进行插值,但出现以下错误。
missing value to go with key
while executing
"dict for {k val} [dict get $config headers] {
_addopts $val config
}"
(procedure "rest::create_interface" line 36)
invoked from within
"rest::create_interface restApi"
(file "t.tcl" line 86)
最初我认为 $headers 字典中的奇数个单词打破了 dict for
。但是从 $token
中删除空格不会改变错误。
在配置数组之外设置值会导致相同的错误:
dict set restApi(callFoo) headers "Auth $token"
我很茫然。请帮助我,Obi-Wan Kenobi。你是我唯一的希望!
2019 年 11 月 30 日更新
事实证明,该实现包括通过用 %
包装变量名称来将变量嵌入到配置字典中的能力。变量的值可以设置为函数的选项:
array set restApi {
callFoo {
url http://example.com/foo
method get
format json
headers "Auth %tokenVar%"
}
}
rest::create_interface restApi
restApi::callFoo -tokenVar $token
还应注意,包允许在调用之前设置这些变量:
restApi::set_static_args tokenVar $token
restApi::callFoo
以这种方式设置的变量适用于所有定义的 REST 调用。因此,如果多个调用需要 Auth
header,只需将 headers "Auth %tokenVar%
添加到每个调用的配置中即可。
以前的答案,为后代保存
感谢 Andreas,他找到了答案。使用以下语法设置 headers 字典有效:
array set restApi {
callFoo {
url http://example.com/foo
method get
format json
}
}
dict set restApi(callFoo) headers [list Auth $token]
这也有效:
array set restApi {
callFoo {
url http://example.com/foo
method get
format json
headers [list Auth $token]
}
}
set restApi(callFoo) [subst $restApi(callFoo)]
后一种形式可能更灵活,因为可以在数组定义中使用多个变量,并且在调用 rest::create_interface
之前,遍历每个元素并执行 subst
.
为了后代,我会注意到我可能曾经尝试过第一种方法,但是因为我有 "commented out" 一些在定义 headers dict 的尝试失败了restApi 数组定义,它 "broke" 数组。那是因为解析器没有将 #headers
line 解释为注释,而是 string #headers
被解释作为字典键。