How to recall this struct? - xcode

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

Related

dynamoDb put item not populating all params

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"`
}

Populate a struct with an array of sub-structs to turn into json

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.

Why am I getting an error when assigning a correct type?

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,
}

Golang - Missing expression error on structs

type Old struct {
UserID int `json:"user_ID"`
Data struct {
Address string `json:"address"`
} `json:"old_data"`
}
type New struct {
UserID int `json:"userId"`
Data struct {
Address string `json:"address"`
} `json:"new_data"`
}
func (old Old) ToNew() New {
return New{
UserID: old.UserID,
Data: { // from here it says missing expression
Address: old.Data.Address,
},
}
}
What is "missing expression" error when using structs?
I am transforming old object to a new one. I minified them just to get straight to the point but the transformation is much more complex. The UserID field for example works great. But when I use struct (which intended to be a JSON object in the end) the Goland IDE screams "missing expression" and the compiler says "missing type in composite literal" on this line. What I am doing wrong? Maybe should I use something else instead of struct? Please help.
Data is an anonymous struct, so you need to write it like this:
type New struct {
UserID int `json:"userId"`
Data struct {
Address string `json:"address"`
} `json:"new_data"`
}
func (old Old) ToNew() New {
return New{
UserID: old.UserID,
Data: struct {
Address string `json:"address"`
}{
Address: old.Data.Address,
},
}
}
(playground link)
I think it'd be cleanest to create a named Address struct.
You're defining Data as an inline struct. When assigning values to it, you must first put the inline declaration:
func (old Old) ToNew() New {
return New{
UserID: old.UserID,
Data: struct {
Address string `json:"address"`
}{
Address: old.Data.Address,
},
}
}
Hence it is generally better to define a separate type for Data, just like User.

How to properly structure struct in struct

I started to learn Go and my (self assigned) mission is to print out a list of hard-coded albums and songs which are on this album.
I've created struct Album which is "object" of album. Inside this struct Album I want to create underlying struct Song. This "object" will hold songs of that album.
My problem/error that I'm getting is ./main.go:46:4: cannot use []Song literal (type []Song) as type Song in field value
This is my code.
// Song struct
type Song struct {
SongName string `json:"song_name"`
Position int `json:"position"`
Length string `json:"length"`
}
// Album struct
type Album struct {
Name string `json:"name"`
Artist string `json:"artist"`
YouTubeLink string `json:"youtube_link"`
AlbumImage string `json:"album_image"`
Description string `json:"description"`
ReleaseDate string `json:"release_date"`
RecordDate string `json:"record_date"`
Length string `json:"length"`
Studio string `json:"studio"`
Songs Song `json:"songs"`
}
func listOfAlbums() []Album {
a := []Album{
{
Name: "The Dark Side of the Moon",
Artist: "Pink Floyd",
YouTubeLink: "https://www.youtube.com/watch?v=HW-lXjOyUWo&list=PLEQdwrAGbxnc9lyYltGRUMt3OgfX0tFbd&index=1",
AlbumImage: "https://upload.wikimedia.org/wikipedia/sl/b/bb/Dsotm.jpg",
Description: "The Dark Side of the Moon is the eighth studio album by English rock band Pink Floyd, released on 1 March 1973 by Harvest Records. Primarily developed during live performances, the band premiered an early version of the record several months before recording began.",
ReleaseDate: "1973.03.01",
RecordDate: "1972.06.01 – 1973.01.01",
Length: "42:32",
Studio: "Abbey Road Studios, London",
//
// Problematic part of code
//
Songs: []Song{
{
SongName: "Speak to Me / Breathe",
Position: 1,
Length: "1:13",
},
{
SongName: "Breathe",
Position: 2,
Length: "2:43",
},
{
SongName: "On the run",
Position: 3,
Length: "3:36",
},
{
SongName: "Time",
Position: 4,
Length: "6:53",
},
{
SongName: "The Great Gig in the Sky",
Position: 5,
Length: "4:36",
},
{
SongName: "Money",
Position: 6,
Length: "6:23",
},
{
SongName: "Us and Them",
Position: 7,
Length: "7:49",
},
{
SongName: "Any Colour You Like",
Position: 8,
Length: "3:26",
},
{
SongName: "Brain Damage",
Position: 9,
Length: "3:49",
},
{
SongName: "Eclipse",
Position: 10,
Length: "2:03",
},
},
},
}
return a
}
Here is a working Go PlayGround code
The questions are following:
Is my logic going in to the right direction? Do I even need to create another struct (Song) or this could be solved in some better manner?
How should I restructure my code so it will be working?
In your Album struct, a single Song is expected for the Songs field. However, []Song is a slice (list type) of songs. When you update your struct declaration with []Song instead of Song, your definition is good.
type Album struct {
Name string `json:"name"`
Artist string `json:"artist"`
YouTubeLink string `json:"youtube_link"`
AlbumImage string `json:"album_image"`
Description string `json:"description"`
ReleaseDate string `json:"release_date"`
RecordDate string `json:"record_date"`
Length string `json:"length"`
Studio string `json:"studio"`
Songs []Song `json:"songs"`
}
Now you can use a []Song{...} for this field, which is also expected in your data definition.
Your code is totally fine and it is normal to use multiple structs for bigger objects that contain other objects.

Resources