dongliuzi3410 2018-07-05 11:16
浏览 52
已采纳

将输出字符串转换为使用golang映射

Get the output as

 {
   ARN: "arn:aws:secretsmanager:us-east-2:xxxx:secret:team_dev-Xhzkt6",
   CreatedDate: 2018-07-05 06:50:07 +0000 UTC,
   Name: "team_dev",
   SecretString: "{\"password\":\"test\"}",
   VersionId: "6b65bfe4-7908-474b-9ae6-xxxx",
   VersionStages: ["AWSCURRENT"]
 }

Try to get the value as map with the key SecretString

d.Set("secret_string", output.SecretString)

How can I get it as Schema TypeMap?

With jq, I can easily get it, but not sure how to do this in golang.

Updates

Thanks, @mkopriva

in the aws sdk (github.com/aws/aws-sdk-go/service/secretsmanager/api.go), output (GetSecretValueOutput) is defined as type:"structure"

type GetSecretValueOutput struct {
        _ struct{} `type:"structure"`

And thanks again to provide a test code, I will try it.

package main

import (
    "fmt"
    "encoding/json"
)

func main() {
    secretString := "{\"password\":\"test\"}"

    sec := map[string]interface{}{}
    if err := json.Unmarshal([]byte(secretString), &sec); err != nil {
        panic(err)
    }
    fmt.Println(sec)
}
  • 写回答

1条回答 默认 最新

  • donglianer5064 2018-07-05 12:54
    关注

    @mkopriva

    Thanks, please answer this question, so I can vote and accept it.

    With your sample code, I did fix the issue.

    -       d.Set("secret_string", output.SecretString)
    +       var sec map[string]interface{}
    +       if err = json.Unmarshal([]byte(*output.SecretString), &sec); err != nil {
    +               return err
    +       }
    +       d.Set("secret_string", sec)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化