Commit 30ce4c7

Richard Luby <richluby@gmail.com>
2016-12-21 14:12:03
reformatting
1 parent 5847217
question.go
@@ -14,24 +14,24 @@ import (
 // returns the index of the newly created category
 func buildTagPath(tags []string) int {
 	currentChosen := getNearestParentCategory(tags)
-	if currentChosen < 0 {//not found in tree
-		for _, tag := range tags{
-			cat := Category{Parent : len(categories)-1,
-			Value : tag}
+	if currentChosen < 0 { //not found in tree
+		for _, tag := range tags {
+			cat := Category{Parent: len(categories) - 1,
+				Value: tag}
 			categories = append(categories, cat)
-			currentChosen = len(categories)-1
+			currentChosen = len(categories) - 1
 		}
 		categories[len(categories)-len(tags)].Parent = -1
 	} else { // some parent found somewhere
 		tagIndex := 0
-		for parentIndex := currentChosen; parentIndex >=0; parentIndex = categories[parentIndex].Parent{
+		for parentIndex := currentChosen; parentIndex >= 0; parentIndex = categories[parentIndex].Parent {
 			tagIndex++ // counting the depth in the tree
 		}
-		for  tagIndex < len(tags){// everything from here on is unkown and needs creation
-			newCat := Category{Parent : currentChosen,
-			Value : tags[tagIndex]}
+		for tagIndex < len(tags) { // everything from here on is unkown and needs creation
+			newCat := Category{Parent: currentChosen,
+				Value: tags[tagIndex]}
 			categories = append(categories, newCat)
-			currentChosen = len(categories) -1
+			currentChosen = len(categories) - 1
 			tagIndex++
 		}
 	}
@@ -51,24 +51,23 @@ func assignCategory(file *os.File) int {
 }
 
 // parseLine parses a csv line and returns a single record
-func parseLine(line string) error {
+func parseLine(line string) (Record, error) {
 	var record Record
 	if strings.HasPrefix(line, "#") {
-		return nil
+		return record, nil
 	} else if strings.Compare(strings.TrimSpace(line), "") == 0 {
-		return nil
+		return record, nil
 	}
 	tokens := strings.Split(line, ",")
 	if len(tokens) < 3 {
-		return fmt.Errorf("Unable to parse line for %s", line)
+		return record, fmt.Errorf("Unable to parse line for %s", line)
 	}
 	record.Question = strings.TrimSpace(tokens[0])
 	record.Answer = strings.TrimSpace(tokens[1])
 	if len(tokens) >= 4 {
 		record.Reference = strings.TrimSpace(tokens[3])
 	}
-	recordsArray = append(recordsArray, record)
-	return nil
+	return record, nil
 }
 
 // loadFile reads the lines of the Answer directory and returns an array of Records
@@ -83,10 +82,13 @@ func LoadFile(file *os.File) error {
 	catIndex := assignCategory(file)
 	scanner := bufio.NewScanner(file)
 	for scanner.Scan() {
-		if err := parseLine(scanner.Text()); err != nil {
+		if record, err := parseLine(scanner.Text()); err != nil {
 			log.Printf("Error while parsing line: %+v.", err)
 		} else {
-			recordsArray[len(recordsArray)-1].Category = catIndex
+			if record.Answer != "" {
+				record.Category = catIndex
+				categories[catIndex].RecordArray = append(categories[catIndex].RecordArray, record)
+			}
 		}
 	}
 	return nil
server.go
@@ -105,7 +105,7 @@ func ExecuteServer() {
 		log.Printf("Failed to load questions due to error: %+v", err)
 		os.Exit(EXIT_CODE.FILE_IO_ERROR)
 	}
-	log.Printf("Loaded %d questions from %s with %d categories.", len(recordsArray), serverConfig.QUESTIONS, len(categories))
+	log.Printf("Loaded %d categories from %s.",  len(categories),serverConfig.QUESTIONS)
 	for _, cat := range categories {
 		log.Printf("Cat: %s", cat.FullCategoryPath())
 	}
serverHandlers.go
@@ -30,7 +30,7 @@ func getRecordForCategory(category string) Record {
 }
 
 // buildRecordArray builds an array of Records based on the parameter criteria
-func buildRecordArray(numQuestions int, categories []int) []Record {
+func buildRecordArray(numQuestions int, categoryIndices []int) []Record {
 	return nil
 }
 
structures.go
@@ -8,17 +8,6 @@ import (
 // API_ROOT defines the root path for the web api interface
 const API_ROOT = "/api"
 
-// recordMap contains all records grouped into
-// categorical buckets. the category serves as
-// the keyname. the map is populated through the
-// loadRecords function call.
-var recordsArray []Record
-
-// usedRecordMap contains the questions that have
-// already been used. when recordMap is empty,
-// usedRecordMap replaces it.
-var usedRecordsArray []Record
-
 // mapLock locks the maps to prevent concurrency issues
 var mapLock *sync.Mutex
 
@@ -101,6 +90,12 @@ type Category struct {
 	// a negative value indicates no parent
 	fullCategoryPath,
 	Value string // leaf portion of path
+	// RecordArray contains all records grouped into
+	// categorical buckets.
+	RecordArray,
+	// usedRecordArray contains the questions that have
+	// already been used.
+	UsedRecordArray []Record
 }
 
 // returns the fully qualified path of this
@@ -189,8 +184,6 @@ func (cSem *CountingSemaphore) SetCapacity(cap int) {
 }
 
 func init() {
-	recordsArray = []Record{}
-	usedRecordsArray = []Record{}
 	categories = []Category{}
 	mapLock = &sync.Mutex{}
 }