Some major changes (#14)
Reviewed-on: #14 Co-authored-by: phoenix <kundeng00@pm.me> Co-committed-by: phoenix <kundeng00@pm.me>
This commit is contained in:
+31
-35
@@ -1,48 +1,49 @@
|
||||
package parser
|
||||
|
||||
import "fmt"
|
||||
import "os"
|
||||
import "unicode"
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
"unicode"
|
||||
|
||||
"git.kundeng.us/phoenix/textsender-models/pkg/contact"
|
||||
)
|
||||
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 {
|
||||
func (prsr *NumberParser) FileDump() []contact.Contact {
|
||||
fmt.Println("Dumping files")
|
||||
objs := []SomeObject{}
|
||||
objs := []contact.Contact{}
|
||||
|
||||
for _, line := range prsr.readLines(prsr.FilePath) {
|
||||
if strings.ContainsAny(line, Letters) {
|
||||
continue
|
||||
}
|
||||
|
||||
myObj := SomeObject{Value: line}
|
||||
if myObj.Value != "" {
|
||||
myObj := contact.Contact{PhoneNumber: line}
|
||||
if myObj.PhoneNumber != "" {
|
||||
objs = append(objs, myObj)
|
||||
}
|
||||
}
|
||||
|
||||
objs = prsr.updateContacts(objs)
|
||||
objs = prsr.removeDups(objs)
|
||||
|
||||
return objs
|
||||
}
|
||||
|
||||
func (prsr NumberParser) UpdateValues(vals []SomeObject) []SomeObject {
|
||||
updated := []SomeObject{}
|
||||
func (prsr *NumberParser) updateContacts(contacts []contact.Contact) []contact.Contact {
|
||||
updated := []contact.Contact{}
|
||||
|
||||
for _, val := range vals {
|
||||
parsed := val.Value
|
||||
for _, ct := range contacts {
|
||||
parsed := ct.PhoneNumber
|
||||
if len(parsed) < 2 {
|
||||
fmt.Println("Invalid number:", parsed)
|
||||
continue
|
||||
@@ -54,49 +55,44 @@ func (prsr NumberParser) UpdateValues(vals []SomeObject) []SomeObject {
|
||||
fmt.Println(parsed)
|
||||
|
||||
updatedParsed := prsr.addPrefix(parsed)
|
||||
updated = append(updated, SomeObject{Value: updatedParsed})
|
||||
updated = append(updated, contact.Contact{PhoneNumber: updatedParsed})
|
||||
}
|
||||
|
||||
return updated
|
||||
}
|
||||
|
||||
func (prsr NumberParser) RemoveDups(vals []SomeObject) []SomeObject {
|
||||
updated := []SomeObject{}
|
||||
func (prsr *NumberParser) removeDups(contacts []contact.Contact) []contact.Contact {
|
||||
updated := []contact.Contact{}
|
||||
uniqueItems := make(map[string]string)
|
||||
|
||||
for _, val := range vals {
|
||||
uniqueItems[val.Value] = val.Value
|
||||
for _, val := range contacts {
|
||||
uniqueItems[val.PhoneNumber] = val.PhoneNumber
|
||||
}
|
||||
|
||||
for _, item := range uniqueItems {
|
||||
updated = append(updated, SomeObject{Value: item})
|
||||
updated = append(updated, contact.Contact{PhoneNumber: item})
|
||||
}
|
||||
|
||||
return updated
|
||||
}
|
||||
|
||||
func (prsr NumberParser) PrintValues(vals []SomeObject, printTotal bool) {
|
||||
fmt.Println("Printing values")
|
||||
func (prsr NumberParser) PrintValues(contacts []contact.Contact, printTotal bool) {
|
||||
fmt.Println("Printing numbers")
|
||||
|
||||
for _, val := range vals {
|
||||
fmt.Println("Value:", val.Value)
|
||||
for _, ct := range contacts {
|
||||
fmt.Println("Number:", ct.PhoneNumber)
|
||||
}
|
||||
|
||||
if printTotal {
|
||||
fmt.Println("Total Numbers:", len(vals))
|
||||
fmt.Println("Total Numbers:", len(contacts))
|
||||
}
|
||||
}
|
||||
|
||||
func (prsr NumberParser) SaveFile(vals []SomeObject) {
|
||||
func (prsr NumberParser) SaveFile(vals []contact.Contact) {
|
||||
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, "", " ")
|
||||
jsonData, err := json.MarshalIndent(&vals, "", " ")
|
||||
if err != nil {
|
||||
fmt.Println("Error marshaling JSON:", err)
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user