I have 2 lambdas that do the exact same thing, however, they are both written using different langages.
1st lambda - runs on a node.js environment, when I create my arguments to putItem, as follows:
const args = {
id: "my id",
__typename: "a type name",
_version: 1,
_lastChangedAt: now.toISOString(),
createdAt: now.toISOString(),
updatedAt: fields.LastModifiedDate
}
var recParams = {
TableName: dynamoTable,
Key: {
"id": Id
},
Item: args,
ReturnValues: "ALL_OLD"
};
and then I use the docClient to insert the row. Everything works fine, all the properties are populated in my dynamo row.
I have the exact same written in Golang:
item := RecentItem{
Id: "some Id",
_version: 1,
__typename: "a type name",
_lastChangedAt: currentTime.UTC().Format("2006-01-02T15:04:05-0700"),
createdAt: currentTime.UTC().Format("2006-01-02T15:04:05-0700"),
updatedAt: currentTime.UTC().Format("2006-01-02T15:04:05-0700"),
}
av, err := dynamodbattribute.MarshalMap(item)
input := &dynamodb.PutItemInput{
Item: av,
TableName: aws.String(tableName),
}
Everything ALMOST works, the item is inserted, but I am missing all the properties except for the id.
Structure declaration :
type RecentItem struct {
Id string `json:"id"`
_version int `json:"_version"`
_lastChangedAt string `json:"_lastChangedAt"`
createdAt string `json:"createdAt"`
updatedAt string `json:"updatedAt"`
}
Not sure why in Go my dynamoDb row is missing properties. Am I missing something?
Properties other than Id must be exported, i.e, started with an Upper case:
type RecentItem struct {
ID string `dynamodbav:"id"`
Version int `dynamodbav:"_version"`
LastChangedAt string `dynamodbav:"_lastChangedAt"`
CreatedAt string `dynamodbav:"createdAt"`
UpdatedAt string `dynamodbav:"updatedAt"`
}
Related
I’m trying to post some JSON. Using the JSON-to-Go tool I have this struct defined:
type IssueSetState struct {
ID string `json:"id"`
CustomFields []struct {
Value struct {
Name string `json:"name"`
} `json:"value"`
Name string `json:"name"`
Type string `json:"$type"`
} `json:"customFields"`
}
I’m trying to populate it with some data that I can then pass into the http library:
jsonValues := &IssueSetState{
ID: resultEntityId.ID,
CustomFields: []{
Value: {
Name: "Fixed",
},
Name: "State",
Type: "StateIssueCustomField",
},
}
jsonEncoded := new(bytes.Buffer)
json.NewEncoder(jsonEncoded).Encode(jsonValues)
I keep getting errors like:
./main.go:245:19: syntax error: unexpected {, expecting type
./main.go:246:9: syntax error: unexpected :, expecting comma or }
./main.go:249:8: syntax error: unexpected : at end of statement
./main.go:251:4: syntax error: unexpected comma after top level declaration
I’m sure the mistake I’m making is a simple one, but I’m new to Go.
One possible way is to define named structs for every anonymous struct you have.
type IssueSetState struct {
ID string `json:"id"`
CustomFields []CustomField `json:"customFields"`
}
type CustomField struct {
Value Value `json:"value"`
Name string `json:"name"`
Type string `json:"type"`
}
type Value struct {
Name string `json:"name"`
}
Now you can create it like this:
IssueSetState{
ID: resultEntityId.ID,
CustomFields: []CustomField{
{
Value: Value{
Name: "Fixed",
},
Name: "State",
Type: "StateIssueCustomField",
},
{
Value: Value{
Name: "Fixed",
},
Name: "State",
Type: "StateIssueCustomField",
},
},
}
So you're initializing the jsonValue badly.
You can fix it in 2 ways:
https://play.golang.org/p/LFO4tOLyG60
making structures flat
https://play.golang.org/p/TyFfaMf7XeF
by repeating the structure definition when declaring value
The first one should be easier and clearer.
Recently I started experimenting with Go, but I hit on hard rock.
I have this type:
type LocationType string
const (
River LocationType = "River"
Mountain LocationType = "Mountain"
)
func (t LocationType) ToString() string {
return string(t)
}
I also have this one:
type LocationCreateInput struct {
Name string `json:"name,omitempty"`
Type *models.LocationType `json:"type,omitempty"`
}
Now I'm trying to create a new LocationCreateInput variable:
input := &gqlModels.LocationCreateInput {
Name: "Test name",
Type: models.River
}
and I am getting the below error:
Cannot use 'models.Site' (type LocationType) as the type *models.LocationType
Can somebody point me to the right way of assigning the Type value? In the end, it is just a string.
What am I missing here? Could you give me a push?
You are trying to assign a value to a pointer type. So it's not "just a string", it's a "a pointer to just a string".
Either you change the type of the struct field from *models.LocationType to models.LocationType, or you need to take the address when assigning:
val := models.River
input := &gqlModels.LocationCreateInput {
Name: "Test name",
Type: &val,
}
I want to relate an entity to itself with GORM
I tried this:
type project struct {
gorm.Model
Name string
ParentID uint
projects []project `gorm:"foreignkey:ParentID,association_foreignkey:ID"`
}
db.Create(&project{Name: "parent", ParentID: 0})
db.Create(&project{Name: "child", ParentID: 1})
db.Create(&project{Name: "child1", ParentID: 1})
var project Project
var projects []Project
db.First(&project)
db.Model(&project).Related(&projects)
but this is the error:
invalid association []
My desired result is:
{Name:"parent",
projects:[
{Name:"child",projects:[]},
{Name:"child1",projects:[]}
]
}
and I want to mention that I am new with golang :)
Change your projects tag to this - gorm:"foreignkey:ParentID" and make the field itself public. To get the parent with children in it do:
p := &project{}
err := db.Where("name = 'parent'").
Preload("Projects").
First(p).Error
how to recall this stryc?
struct User {
var name: String
var street: String
var city: String
var postalCode: String
func printAddress() -> String {
return """
\(name)
\(street)
\(city)
\(postalCode)
"""
}
}
I expect to have an address in different lines like the method, for example
Well Smith
streetnumber this one
lalaland
19890
but result comes back in struct form
Not sure what you mean with "recall struct" but if you want to print it in the expected format simply use printAddress()
let user = User(name: "name", street: "street", city: "city", postalCode: "postalCode")
print(user.printAddress())
Output:
name
street
city
postalCode
I am trying to take a cloudwatch_event and get it into a go struct. I have a CloudwatchEvent struct and inside of that is a blob of json that I need to get into another struct. The first level of the struct seems to work fine, but there is a parsing error when it tries to access the nested json.
This is my sample event. I am trying to get down to detail > EC2InstanceId I think I will also need the status code.
{
"version": "0",
"id": "3e3c153a-8339-4e30-8c35-687ebef853fe",
"detail-type": "EC2 Instance Launch Successful",
"source": "aws.autoscaling",
"account": "123456789012",
"time": "2015-11-11T21:31:47Z",
"region": "us-east-1",
"resources": [
"arn:aws:autoscaling:us-east-1:123456789012:autoScalingGroup:eb56d16b-bbf0-401d-b893-d5978ed4a025:autoScalingGroupName/sampleLuanchSucASG",
"arn:aws:ec2:us-east-1:123456789012:instance/i-b188560f"
],
"detail": {
"StatusCode": "InProgress",
"AutoScalingGroupName": "sampleLuanchSucASG",
"ActivityId": "9cabb81f-42de-417d-8aa7-ce16bf026590",
"Details": {
"Availability Zone": "us-east-1b",
"Subnet ID": "subnet-95bfcebe"
},
"RequestId": "9cabb81f-42de-417d-8aa7-ce16bf026590",
"EndTime": "2015-11-11T21:31:47.208Z",
"EC2InstanceId": "i-b188560f",
"StartTime": "2015-11-11T21:31:13.671Z",
"Cause": "At 2015-11-11T21:31:10Z a user request created an AutoScalingGroup changing the desired capacity from 0 to 1. At 2015-11-11T21:31:11Z an instance was started in response to a difference between desired and actual capacity, increasing the capacity from 0 to 1."
}
}
Since the aws-go-lambda library does not seem to handle these events i created two structs.
type CloudWatchEvent struct {
Version string `json:"version"`
ID string `json:"id"`
DetailType string `json:"detail-type"`
Source string `json:"source"`
AccountID string `json:"account"`
Time time.Time `json:"time"`
Region string `json:"region"`
Resources []string `json:"resources"`
Detail CloudWatchDetails `json:"detail"`
}
type CloudWatchDetails struct {
Detail struct {
StatusCode string `json:"StatusCode"`
AutoScalingGroupName string `json:"AutoScalingGroupName"`
ActivityID string `json:"ActivityId"`
Details struct {
AvailabilityZone string `json:"Availability Zone"`
SubnetID string `json:"Subnet ID"`
} `json:"Details"`
RequestID string `json:"RequestId"`
EndTime time.Time `json:"EndTime"`
EC2InstanceID string `json:"EC2InstanceId"`
StartTime time.Time `json:"StartTime"`
Cause time.Time `json:"Cause"`
} `json:"detail"`
}
I seem to be able to address event.Version or event.Id fine but when I try and address event.Detail.EC2InstanceId I get what looks like a byte object.
You were double nesting Detail property. Also "Cause" property in the JSON is a string and not a time.Time, you might want to change it.
That should do the trick.
type CloudWatchEvent struct {
Version string `json:"version"`
ID string `json:"id"`
DetailType string `json:"detail-type"`
Source string `json:"source"`
AccountID string `json:"account"`
Time time.Time `json:"time"`
Region string `json:"region"`
Resources []string `json:"resources"`
Detail CloudWatchDetails `json:"detail"`
}
type CloudWatchDetails struct {
StatusCode string `json:"StatusCode"`
AutoScalingGroupName string `json:"AutoScalingGroupName"`
ActivityID string `json:"ActivityId"`
Details struct {
AvailabilityZone string `json:"Availability Zone"`
SubnetID string `json:"Subnet ID"`
} `json:"Details"`
RequestID string `json:"RequestId"`
EndTime time.Time `json:"EndTime"`
EC2InstanceID string `json:"EC2InstanceId"`
StartTime time.Time `json:"StartTime"`
Cause string `json:"Cause"`
}
Code in the playground