登录
首页 >  Golang >  Go问答

生成 DynamoDBEvent 属性值的 JSON

来源:stackoverflow

时间:2024-02-09 10:23:42 405浏览 收藏

有志者,事竟成!如果你在学习Golang,那么本文《生成 DynamoDBEvent 属性值的 JSON》,就很适合你!文章讲解的知识点主要包括,若是你对本文感兴趣,或者是想搞懂其中某个知识点,就请你继续往下看吧~

问题内容

我正在编写一个 lambda,它从 dynamodb 表接收流数据。解析出正确的记录后,我尝试将其转换为 json。目前,我正在这样做:

func lambdahandler(ctx context.context, request events.dynamodbevent) error {

    // ...

    // not actual code, just for demonstration
    record = request.records[0]

    data, err := events.newmapattribute(record.change.newimage).marshaljson()
    if err != nil {
        return err
    }

    // ...
}

问题是这会产生如下所示的 json 负载:

{
  "m": {
    "action": { "n": "0" },
    "expiration": { "n":"0" },
    "id": { "s": "trades|v4|2023-02-08" },
    "order": { "n":"22947407" },
    "price": { "n":"96.139" },
    "sort_key": { "s":"22947407" },
    "stop_limit": { "n":"0" },
    "stop_loss": { "n":"96.7" },
    "symbol": { "s":"cadjpy" },
    "take_profit": { "n":"94.83" },
    "type": { "n":"5" },
    "type_filling": { "n":"0" },
    "type_time": { "n":"0" },
    "volume": { "n":"1" }
  }
}

如您所见,这模仿了 dynamodb 属性值的结构,但这不是我想要的。相反,我尝试生成如下所示的 json 有效负载:

{
  "action": 0,
  "expiration": 0,
  "id": "trades|v4|2023-02-08",
  "order": 22947407, 
  "price": 96.139,
  "sort_key": "22947407",
  "stop_limit": 0,
  "stop_loss": 96.7,
  "symbol": "CADJPY",
  "take_profit": 94.83,
  "type": 5,
  "type_filling": 0,
  "type_time": 0,
  "volume": 1
}
现在,我可以想到几种方法来做到这一点:将 record.change.newimage 中的值硬编码到 map[interface{}] 中,然后使用 json.marshal 对其进行编组,但有效负载的类型我receive 可以是几种不同类型之一。我也可以使用反射来做同样的事情,但我不想花时间调试反射代码。亚马逊有可用的功能来执行此操作吗?看起来应该有,但我找不到任何东西。

正确答案


我最终编写了一个函数,它或多或少地完成了我需要的功能。这会将数值写入字符串,但否则会生成我正在寻找的 json 有效负载:

// AttributesToJSON attempts to convert a mapping of DynamoDB attribute values to a properly-formatted JSON string
func AttributesToJSON(attrs map[string]events.DynamoDBAttributeValue) ([]byte, error) {

    // Attempt to map the DynamoDB attribute value mapping to a map[string]interface{}
    // If this fails then return an error
    keys := make([]string, 0)
    mapping, err := toJSONInner(attrs, keys...)
    if err != nil {
        return nil, err
    }

    // Attempt to convert this mapping to JSON and return the result
    return json.Marshal(mapping)
}

// Helper function that converts a struct to JSON field-mapping
func toJSONInner(attrs map[string]events.DynamoDBAttributeValue, keys ...string) (map[string]interface{}, error) {
    jsonStr := make(map[string]interface{})
    for key, attr := range attrs {

        // Attempt to convert the field to a JSON mapping; if this fails then return an error
        casted, err := toJSONField(attr, append(keys, key)...)
        if err != nil {
            return nil, err
        }

        // Set the field to its associated key in our mapping
        jsonStr[key] = casted
    }

    return jsonStr, nil
}

// Helper function that converts a specific DynamoDB attribute value to its JSON value equivalent
func toJSONField(attr events.DynamoDBAttributeValue, keys ...string) (interface{}, error) {
    attrType := attr.DataType()
    switch attrType {
    case events.DataTypeBinary:
        return attr.Binary(), nil
    case events.DataTypeBinarySet:
        return attr.BinarySet(), nil
    case events.DataTypeBoolean:
        return attr.Boolean(), nil
    case events.DataTypeList:

        // Get the list of items from the attribute value
        list := attr.List()

        // Attempt to convert each item in the list to a JSON mapping
        data := make([]interface{}, len(list))
        for i, item := range list {

            // Attempt to map the field to a JSON mapping; if this fails then return an error
            casted, err := toJSONField(item, keys...)
            if err != nil {
                return nil, err
            }

            // Set the value at this index to the mapping we generated
            data[i] = casted
        }

        // Return the list we created
        return data, nil
    case events.DataTypeMap:
        return toJSONInner(attr.Map(), keys...)
    case events.DataTypeNull:
        return nil, nil
    case events.DataTypeNumber:
        return attr.Number(), nil
    case events.DataTypeNumberSet:
        return attr.NumberSet(), nil
    case events.DataTypeString:
        return attr.String(), nil
    case events.DataTypeStringSet:
        return attr.StringSet(), nil
    default:
        return nil, fmt.Errorf("Attribute at %s had unknown attribute type of %d",
            strings.Join(keys, "."), attrType)
    }
}

此代码的工作原理是迭代顶级映射中的每个键和值,并将值转换为 interface{},然后将结果转换为 json。在这种情况下,interface{} 可以是 []byte[][]bytestring[]stringboolinterface{} 或 phpcnc phpcnmap[string]interface{} 取决于类型属性值。

理论要掌握,实操不能落!以上关于《生成 DynamoDBEvent 属性值的 JSON》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注golang学习网公众号吧!

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