登录
首页 >  Golang >  Go问答

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

来源:stackoverflow

时间:2024-04-14 17:33:35 214浏览 收藏

一分耕耘,一分收获!既然打开了这篇文章《如何使用flask_restplus定义字典字段以在使用swagger codegen生成的go代码中使用?》,就坚持看下去吧!文中内容包含等等知识点...希望你能在阅读本文后,能真真实实学到知识或者帮你解决心中的疑惑,也欢迎大佬或者新人朋友们多留言评论,多给建议!谢谢!

问题内容

我使用 swagger cli 生成 go 代码来调用我的 flask 应用程序。 swagger codegen 将使用 flask_restplus 模型定义的 fields.raw 类型转换为 go 中的 *interface{}

在go中将值赋给*interface{}类型的字段会返回

prog.go:18:26:无法使用notebook_spec_secrets(类型 map[string]string) 作为赋值中的类型 *interface {}:*interface {} 是指向接口的指针,而不是接口

你可以在这里测试一下 https://play.golang.org/p/sfe9qr-72_g

一个快速而肮脏的修复将通过 swagger cli 更改生成的代码并进行更改

notebookspec *接口{}

notebookspec 接口{}

  1. 是否可以将字典转换为go中的*interface{}? (我的google搜索显示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://@.dfs.core.windows.net/folder1",
                                          })})

解决方案


我不确定为什么它会生成一个指向接口的指针,但仍然可以通过将映射显式转换为 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

理论要掌握,实操不能落!以上关于《如何使用flask_restplus定义字典字段以在使用swagger codegen生成的go代码中使用?》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注golang学习网公众号吧!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>