go - 如何使用 flask_restplus 定义字典字段以用于使用 swagger codegen 生成的 go 代码?

标签 go swagger-codegen flask-restplus

我已经使用 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{}

  1. 是否可以在 go 中将字典转换为 *interface{}? (我的谷歌搜索显示指向接口(interface)的指针在 go 中是无效的,逻辑上是不正确的)

  2. 如何使用 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

https://play.golang.org/p/rHrMH_jF_oS

关于go - 如何使用 flask_restplus 定义字典字段以用于使用 swagger codegen 生成的 go 代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55980178/

相关文章:

unit-testing - 运行 Ginkgo 测试套件(在运行任何规范之前设置 BeforeSuite

java - 使用 LinkedHashMap 而不是 HashMap 用于 Swagger 字典类型(Java codegen)?

jmeter - Swagger Codegen JMeter 测试数据模板

python - 如何处理 Flask-Restplus 响应类中的多个 'GET' 端点

http - 如何在 flask restplus swagger 中记录 HTTP POST/PUT 数据二进制上传?

python - 从模型中获取 JSONAPI 模式

http - golang 接受 gzip 后的数据

amazon-web-services - dynamodb updateitem 与条件表达式返回项目是否已更新

go - go中的任意JSON数据结构

java - 来自现有 JSON 的 Springfox Swagger Ui