Skip to content

Commit

Permalink
Fix struct embed exercise
Browse files Browse the repository at this point in the history
  • Loading branch information
rg0now committed Aug 28, 2024
1 parent e4387f4 commit 9f16135
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 47 deletions.
20 changes: 5 additions & 15 deletions 19-structs/03-struct-embedding/.README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,7 @@ The first struct represents a book with an author. The JSON formatted structure
"title": "The Go Programming Language",
"author": {
"name": "Alan A. A. Donovan",
{{if eq (index . "field") "email"}}
"email": "[email protected]"
{{end}}
{{if eq (index . "field") "address"}}
"address": "103 W Vandalia St, Edwardsville, Indiana, USA"
{{end}}
{{if eq (index . "field") "email"}}"email": "[email protected]"{{end}}{{if eq (index . "field") "address"}}"address": "103 W Vandalia St, Edwardsville, Indiana, USA"{{end}}
},
"pages": 380,
"ISBN": "978-0134190440"
Expand All @@ -40,12 +35,7 @@ The second struct should represent an article in a journal. The serialized JSON
"title": "Smashing The Kernel Stack For Fun And Profit",
"author": {
"name": "Sinan Eren",
{{if eq (index . "field") "email"}}
"email": "[email protected]"
{{end}}
{{if eq (index . "field") "address"}}
"address": "12031 N Tatum Blvd, Phoenix, Arkansas, USA"
{{end}}
{{if eq (index . "field") "email"}}"email": "[email protected]"{{end}}{{if eq (index . "field") "address"}}"address": "12031 N Tatum Blvd, Phoenix, Arkansas, USA"{{end}}
},
"journal": "Phrack Magazine"
"year": 2002,
Expand All @@ -59,9 +49,9 @@ func ParseArticle(jsonData []byte) (Article, error)
```

Requirements:
1. Define an Author and a Journal struct with Name and {{if eq (index . "field") "email"}}Email{{end}}{{if eq (index . "field") "address"}}Address{{end}} fields.
2. Define a Book and a Journal struct that both embed the Author struct to represent author info.
3. Implement the `ParseBook` and `ParseArticle` functions to parse the JSON data into a Book or an Article struct.
1. Define an `Author` struct with Name and {{if eq (index . "field") "email"}}Email{{end}}{{if eq (index . "field") "address"}}Address{{end}} fields.
2. Define a `Book` and an `Article` struct that both embed the `Author` struct to represent author info.
3. Implement the `ParseBook` and `ParseArticle` functions to parse the JSON data into a `Book` or an `Article` struct.
4. Use appropriate tags on your struct fields to match the JSON keys.

Place your code in the `exercise.go` file near tho the placeholder `// INSERT YOUR CODE HERE`
Expand Down
44 changes: 12 additions & 32 deletions 19-structs/03-struct-embedding/.exercise_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,7 @@ func TestParseBook(t *testing.T) {
"title": "The Go Programming Language",
"author": {
"name": "Alan A. A. Donovan",
{{if eq (index . "field") "email"}}
"email": "[email protected]"
{{end}}
{{if eq (index . "field") "address"}}
"address": "103 W Vandalia St, Edwardsville, Indiana, USA"
{{end}}
{{if eq (index . "field") "email"}}"email": "[email protected]"{{end}}{{if eq (index . "field") "address"}}"address": "103 W Vandalia St, Edwardsville, Indiana, USA"{{end}}
},
"pages": 380,
"ISBN": "978-0134190440"
Expand All @@ -26,12 +21,7 @@ func TestParseBook(t *testing.T) {
Title: "The Go Programming Language",
Author: Author{
Name: "Alan A. A. Donovan",
{{if eq (index . "field") "email"}}
Email: "[email protected]",
{{end}}
{{if eq (index . "field") "address"}}
Address: "103 W Vandalia St, Edwardsville, Indiana, USA",
{{end}}
{{if eq (index . "field") "email"}}Email: "[email protected]"{{end}}{{if eq (index . "field") "address"}}Address: "103 W Vandalia St, Edwardsville, Indiana, USA"{{end}},
},
Pages: 380,
ISBN: "978-0134190440",
Expand All @@ -58,31 +48,21 @@ func TestParseBookInvalidInput(t *testing.T) {

func TestParseArticle(t *testing.T) {
jsonData := []byte(`
{
"title": "Smashing The Kernel Stack For Fun And Profit",
"author": {
"name": "Sinan Eren",
{{if eq (index . "field") "email"}}
"email": "[email protected]"
{{end}}
{{if eq (index . "field") "address"}}
"address": "12031 N Tatum Blvd, Phoenix, Arkansas, USA"
{{end}}
},
"journal": "Phrack Magazine"
"year": 2002,
}`)
{
"title": "Smashing The Kernel Stack For Fun And Profit",
"author": {
"name": "Sinan Eren",
{{if eq (index . "field") "email"}}"email": "[email protected]"{{end}}{{if eq (index . "field") "address"}}"address": "12031 N Tatum Blvd, Phoenix, Arkansas, USA"{{end}}
},
"journal": "Phrack Magazine",
"year": 2002
}`)

expected := Article{
Title: "Smashing The Kernel Stack For Fun And Profit",
Author: Author{
Name: "Sinan Eren",
{{if eq (index . "field") "email"}}
Email: "[email protected]",
{{end}}
{{if eq (index . "field") "address"}}
Address: "12031 N Tatum Blvd, Phoenix, Arkansas, USA",
{{end}}
{{if eq (index . "field") "email"}}Email: "[email protected]"{{end}}{{if eq (index . "field") "address"}}Address: "12031 N Tatum Blvd, Phoenix, Arkansas, USA"{{end}},
},
Journal: "Phrack Magazine",
Year: 2002,
Expand Down

0 comments on commit 9f16135

Please sign in to comment.