package parser import "fmt" import "os" import "unicode" import ( "bufio" "log" ) import "encoding/json" import "strings" const Letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" type SomeObject struct { Value string `json:"value"` } type NumberParser struct { FilePath string } func (prsr NumberParser) FileDump() []SomeObject { fmt.Println("Dumping files") objs := []SomeObject{} for _, line := range prsr.readLines(prsr.FilePath) { if strings.ContainsAny(line, Letters) { continue } myObj := SomeObject{Value: line} if myObj.Value != "" { objs = append(objs, myObj) } } return objs } func (prsr NumberParser) UpdateValues(vals []SomeObject) []SomeObject { updated := []SomeObject{} for _, val := range vals { parsed := val.Value if len(parsed) < 2 { fmt.Println("Invalid number:", parsed) continue } pars := prsr.removeSomeData(parsed) parsed = pars fmt.Println(parsed) updatedParsed := prsr.addPrefix(parsed) updated = append(updated, SomeObject{Value: updatedParsed}) } return updated } func (prsr NumberParser) RemoveDups(vals []SomeObject) []SomeObject { updated := []SomeObject{} uniqueItems := make(map[string]string) for _, val := range vals { uniqueItems[val.Value] = val.Value } for _, item := range uniqueItems { updated = append(updated, SomeObject{Value: item}) } return updated } func (prsr NumberParser) PrintValues(vals []SomeObject, printTotal bool) { fmt.Println("Printing values") for _, val := range vals { fmt.Println("Value:", val.Value) } if printTotal { fmt.Println("Total Numbers:", len(vals)) } } func (prsr NumberParser) SaveFile(vals []SomeObject) { filename := "numbers.json" allNumbers := []string{} for _, val := range vals { allNumbers = append(allNumbers, val.Value) } // Create json and save it to the filesystem jsonData, err := json.MarshalIndent(allNumbers, "", " ") if err != nil { fmt.Println("Error marshaling JSON:", err) return } err = os.WriteFile(filename, jsonData, 0644) if err != nil { fmt.Println("Error writing file:", err) return } } func (prsr NumberParser) readLines(filename string) []string { // Open the file file, err := os.Open(filename) if err != nil { log.Fatal(err) } defer file.Close() // Create a scanner to read the file line by line scanner := bufio.NewScanner(file) lines := []string{} // Read line by line for scanner.Scan() { line := scanner.Text() lines = append(lines, line) } // Check for any errors during scanning if err := scanner.Err(); err != nil { log.Fatal(err) } return lines } func (prsr NumberParser) removeSomeData(unparsed string) string { parsed := "" for _, ch := range unparsed { if ch == ' ' { continue } else { if unicode.IsDigit(ch) { parsed += string(ch) } } } fmt.Println("Parsed:", parsed) return parsed } func (prsr NumberParser) addPrefix(raw string) string { parsed := raw firstChar := raw[0] secondChar := raw[1] if firstChar == '1' { parsed = "+" + parsed } else if firstChar != '+' { if secondChar != '1' { parsed = "+1" + parsed } else { parsed = "+1" + parsed } } return parsed }