我是新手,我必须使用 aws go sdk 从 SQS 读取 AWS Config 通知。 (AWS 配置服务 -> sns -> sqs) 我能够收到消息。但我想进入消息获取资源类型、resourceId、awsRegion 等信息。 这是我的示例消息字符串(字符串化 json)。 https://gist.github.com/HarishAtGitHub/fcbb01515d11044d04bde14a3d9f6e7a
我是 python 背景的,在 python 中这很容易做到,因为 json 就像一本字典。我们可以通过嵌套索引轻松获取。 但在 go 中,我似乎应该使用正确的结构来理解这条消息。
有人可以指出正确的结构或关于如何在消息中获取不同属性的任何想法吗?
最佳答案
我总是使用 this从 JSON blob 生成结构定义的工具。您唯一需要自己做的就是为 null 值选择一个类型:它显然无法确定这些值,因此将它们设置为 interface{}(任何类型实现这个)。
如果您只想解码,则可以省略结构标记(例如 json:"changeType"),而不是相反。
type ConfigNotification struct {
ConfigurationItemDiff struct {
ChangedProperties struct {
} `json:"changedProperties"`
ChangeType string `json:"changeType"`
} `json:"configurationItemDiff"`
ConfigurationItem struct {
RelatedEvents []interface{} `json:"relatedEvents"`
Relationships []interface{} `json:"relationships"`
Configuration struct {
Attachments []interface{} `json:"attachments"`
AvailabilityZone string `json:"availabilityZone"`
CreateTime time.Time `json:"createTime"`
Encrypted bool `json:"encrypted"`
KmsKeyID interface{} `json:"kmsKeyId"`
Size int `json:"size"`
SnapshotID string `json:"snapshotId"`
State string `json:"state"`
VolumeID string `json:"volumeId"`
Iops int `json:"iops"`
Tags []struct {
Key string `json:"key"`
Value string `json:"value"`
} `json:"tags"`
VolumeType string `json:"volumeType"`
} `json:"configuration"`
SupplementaryConfiguration struct {
} `json:"supplementaryConfiguration"`
Tags struct {
Creator string `json:"creator"`
} `json:"tags"`
ConfigurationItemVersion string `json:"configurationItemVersion"`
ConfigurationItemCaptureTime time.Time `json:"configurationItemCaptureTime"`
ConfigurationStateID int64 `json:"configurationStateId"`
AwsAccountID string `json:"awsAccountId"`
ConfigurationItemStatus string `json:"configurationItemStatus"`
ResourceType string `json:"resourceType"`
ResourceID string `json:"resourceId"`
ResourceName interface{} `json:"resourceName"`
ARN string `json:"ARN"`
AwsRegion string `json:"awsRegion"`
AvailabilityZone string `json:"availabilityZone"`
ConfigurationStateMd5Hash string `json:"configurationStateMd5Hash"`
ResourceCreationTime time.Time `json:"resourceCreationTime"`
} `json:"configurationItem"`
NotificationCreationTime time.Time `json:"notificationCreationTime"`
MessageType string `json:"messageType"`
RecordVersion string `json:"recordVersion"`
}
关于amazon-web-services - 读取 AWS Config 通知的正确结构是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51460711/
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
我有一个模型:classItem项目有一个属性“商店”基于存储的值,我希望Item对象对特定方法具有不同的行为。Rails中是否有针对此的通用设计模式?如果方法中没有大的if-else语句,这是如何干净利落地完成的? 最佳答案 通常通过Single-TableInheritance. 关于ruby-on-rails-Rails-子类化模型的设计模式是什么?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.co
我想将html转换为纯文本。不过,我不想只删除标签,我想智能地保留尽可能多的格式。为插入换行符标签,检测段落并格式化它们等。输入非常简单,通常是格式良好的html(不是整个文档,只是一堆内容,通常没有anchor或图像)。我可以将几个正则表达式放在一起,让我达到80%,但我认为可能有一些现有的解决方案更智能。 最佳答案 首先,不要尝试为此使用正则表达式。很有可能你会想出一个脆弱/脆弱的解决方案,它会随着HTML的变化而崩溃,或者很难管理和维护。您可以使用Nokogiri快速解析HTML并提取文本:require'nokogiri'h
大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje
我正在寻找执行以下操作的正确语法(在Perl、Shell或Ruby中):#variabletoaccessthedatalinesappendedasafileEND_OF_SCRIPT_MARKERrawdatastartshereanditcontinues. 最佳答案 Perl用__DATA__做这个:#!/usr/bin/perlusestrict;usewarnings;while(){print;}__DATA__Texttoprintgoeshere 关于ruby-如何将脚
我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
为什么4.1%2返回0.0999999999999996?但是4.2%2==0.2。 最佳答案 参见此处:WhatEveryProgrammerShouldKnowAboutFloating-PointArithmetic实数是无限的。计算机使用的位数有限(今天是32位、64位)。因此计算机进行的浮点运算不能代表所有的实数。0.1是这些数字之一。请注意,这不是与Ruby相关的问题,而是与所有编程语言相关的问题,因为它来自计算机表示实数的方式。 关于ruby-为什么4.1%2使用Ruby返
我正在查看instance_variable_set的文档并看到给出的示例代码是这样做的:obj.instance_variable_set(:@instnc_var,"valuefortheinstancevariable")然后允许您在类的任何实例方法中以@instnc_var的形式访问该变量。我想知道为什么在@instnc_var之前需要一个冒号:。冒号有什么作用? 最佳答案 我的第一直觉是告诉你不要使用instance_variable_set除非你真的知道你用它做什么。它本质上是一种元编程工具或绕过实例变量可见性的黑客攻击
好的,所以我的目标是轻松地将一些数据保存到磁盘以备后用。您如何简单地写入然后读取一个对象?所以如果我有一个简单的类classCattr_accessor:a,:bdefinitialize(a,b)@a,@b=a,bendend所以如果我从中非常快地制作一个objobj=C.new("foo","bar")#justgaveitsomerandomvalues然后我可以把它变成一个kindaidstring=obj.to_s#whichreturns""我终于可以将此字符串打印到文件或其他内容中。我的问题是,我该如何再次将这个id变回一个对象?我知道我可以自己挑选信息并制作一个接受该信