生成 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
、[][]byte
、string
、[]string
、bool
、interface{}
或 phpcnc phpcnmap[string]interface{} 取决于类型属性值。
理论要掌握,实操不能落!以上关于《生成 DynamoDBEvent 属性值的 JSON》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注golang学习网公众号吧!
声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
-
502 收藏
-
502 收藏
-
501 收藏
-
501 收藏
-
501 收藏
最新阅读
更多>
-
139 收藏
-
204 收藏
-
325 收藏
-
477 收藏
-
486 收藏
-
439 收藏
-
357 收藏
-
352 收藏
-
101 收藏
-
440 收藏
-
212 收藏
-
143 收藏
课程推荐
更多>
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 508次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习