-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
17 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
|
@@ -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, | ||
|
@@ -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` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
|
@@ -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", | ||
|
@@ -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, | ||
|