我已经使用 swagger CLI 生成 go 代码来调用我的 flask 应用程序。 swagger codegen 将使用 flask_restplus
模型定义的 fields.Raw
类型转换为 *interface{}
in go
在 go 中使用 *interface{} type 将值赋值给字段返回
prog.go:18:26: cannot use notebook_spec_secrets (type map[string]string) as type *interface {} in assignment: *interface {} is pointer to interface, not interface
你可以在这里测试 https://play.golang.org/p/sFE9Qr-72_G
一个快速而肮脏的修复方法是通过 swagger cli 和 change 更改生成的代码
NotebookSpec *interface{}
到
NotebookSpec interface{}
是否可以在 go 中将字典转换为 *interface{}? (我的谷歌搜索显示指向接口(interface)的指针在 go 中是无效的,逻辑上是不正确的)
如何使用 flask-restplus 定义字典字段
run_definition = api.model('Run definition',
{
'notebook_spec_secrets':
fields.Raw(required=False,
example={
"eventhub_source_cs": "Endpoint=sb://xxxx.servicebus.windows.net/;SharedAccessKeyName=xxxx;SharedAccessKey=xxxx=;EntityPath=sourceeh",
"eventhub_destination_cs": "Endpoint=sb://xxxx.servicebus.windows.net/;SharedAccessKeyName=xxxx;SharedAccessKey=xxxx=;EntityPath=desteh",
"adl2_destination_oauth2_clientid": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
"adl2_destination_oauth2_clientsecret": "xxxx=",
"adl2_destination_oauth2_tenantid": "xxxx=",
"adl2_destination_cs": "abfss://<file-system-name>@<storage-account-name>.dfs.core.windows.net/folder1",
})})
最佳答案
我不确定为什么它会生成一个指向接口(interface)的指针,但仍然可以通过将您的映射显式转换为 interface{}
然后获取地址来分配给它
那:
notebook_spec_secrets := map[string]string{
"eventhub_source_cs": "1",
"eventhub_destination_cs": "2",
"adl2_destination_oauth2_clientid": "3",
"adl2_destination_oauth2_clientsecret": "4",
"adl2_destination_oauth2_tenantid": "5",
"adl2_destination_cs": "6",
}
var nssi interface{} = notebook_spec_secrets
definition.NotebookSpec = &nssi
关于go - 如何使用 flask_restplus 定义字典字段以用于使用 swagger codegen 生成的 go 代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55980178/