This is a simplified example based off of a small personal project, but I'm having trouble designing something like this at a larger scale in clean testable go code. I'm feeling completely hamstrung without constructors, inheritance, and the like. Everything I've found so far has been either a trivial example or wildly complex, so I'm struggling to put this together.
The PopulateLibrary() function will be called in parallel using a waitGroup
, to insert 50 books in parallel.
// struct that book inserter API accepts
type InsertBookTemplate struct {
name string
genre string
subGenre string
type string
authorBio AuthorBio
chapters []*Chapter{}
}
type AuthorBio struct {
bio string
school string
}
type Chapter struct {
name string
description string
}
// to be called in parallel with waitGroups
func PopulateLibrary(title, genre, author, publisher, description, language string) string {
// set some default vars that all books use
subGenre := getSubGenre(genre, author)
authorBio, authorSchool := getAuthorInfo(author, publisher)
publisher := "Published by: " + publisher
translations := translationsAvailable(language)
type := "hardcover"
numChapters := 10
//override defaults per genre
switch genre{
case "standard":
case "cooking":
translations = ""
case "math":
translations = ""
numChapters = 20
case "sci-fi":
translations = "spanish german"
type = "softcover"
if subGenre == "" {
subGenre = "new sci-fi"
} else {
subGenre = "cyberpunk"
}
case "horror":
translations = "Spanish"
case "child":
numChapters = 3
}
newBook := &InsertBookTemplate{}
newBook.name = title
nweBook.genre = genre
newBook.subGenre = subGenre
newBook.type = type
newAuthorBio := &authorBio{}
newAuthorBio.bio = authorBio
newAuthorBio.school = authorSchool
newBook.authorBio = newAuthorBio
chapters := []*Chapter{}
for i:=0; i <= numChapters; i++ {
chapter := &Chapter{}
chapter.name = getSomeName(i)
chapter.description = getSomeDescription(i)
chapters = append(chapters, chapter)
}
newBook.chapters := []*Chapter{}
newBook.chapters := append(newBook.chapters, chapters...)
InsertNewBook(newBook)
}
Aucun commentaire:
Enregistrer un commentaire