Changed format (#13)
Reviewed-on: #13 Co-authored-by: phoenix <kundeng00@pm.me> Co-committed-by: phoenix <kundeng00@pm.me>
This commit is contained in:
+15
-63
@@ -4,12 +4,9 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"git.kundeng.us/phoenix/textsender-models/pkg/contact"
|
||||
"git.kundeng.us/phoenix/textsender-models/pkg/message"
|
||||
|
||||
"git.kundeng.us/phoenix/sender/models"
|
||||
)
|
||||
|
||||
func GetPaths(args []string) (configPath string, numberPath string, messagePath string) {
|
||||
@@ -20,86 +17,41 @@ func GetPaths(args []string) (configPath string, numberPath string, messagePath
|
||||
return
|
||||
}
|
||||
|
||||
func ParseNumbers(filepath string) (numbers [](models.Number)) {
|
||||
nmbrs := [](string){}
|
||||
func ParseNumbers(filepath string) (numbers [](contact.Contact), err error) {
|
||||
var nmbrs []contact.Contact
|
||||
|
||||
content, err := os.ReadFile(filepath)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println("An error occurred")
|
||||
return numbers
|
||||
return numbers, err
|
||||
} else {
|
||||
if err = json.Unmarshal(content, &nmbrs); err != nil {
|
||||
fmt.Println("An error occurred:", err)
|
||||
return numbers, err
|
||||
} else {
|
||||
return nmbrs, nil
|
||||
}
|
||||
}
|
||||
|
||||
err = json.Unmarshal(content, &nmbrs)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println("An error occurred")
|
||||
return numbers
|
||||
}
|
||||
|
||||
for _, nbr := range nmbrs {
|
||||
var number models.Number
|
||||
number.PhoneNumber = nbr
|
||||
numbers = append(numbers, number)
|
||||
}
|
||||
|
||||
return numbers
|
||||
}
|
||||
|
||||
func ParseMessage(filepath string) (message models.Message) {
|
||||
msg := [](string){""}
|
||||
func ParseMessage(filepath string) (*message.Message, error) {
|
||||
var msg message.Message
|
||||
|
||||
content, err := os.ReadFile(filepath)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println("An error occurred")
|
||||
return
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = json.Unmarshal(content, &msg)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println("An error occurred")
|
||||
return
|
||||
return nil, err
|
||||
}
|
||||
|
||||
message.Text = strings.Join(msg, "")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func Convert(data interface{}, msgOrNumber int) (interface{}, error) {
|
||||
switch msgOrNumber {
|
||||
case 0:
|
||||
{
|
||||
if msg, ok := data.(models.Message); ok {
|
||||
return message.Message{Content: msg.Text}, nil
|
||||
} else {
|
||||
return nil, fmt.Errorf("Could not resolve value")
|
||||
}
|
||||
}
|
||||
case 1:
|
||||
{
|
||||
var items []contact.Contact
|
||||
|
||||
switch nbrs := data.(type) {
|
||||
case []models.Number:
|
||||
for _, number := range nbrs {
|
||||
if !number.IsEmpty() {
|
||||
items = append(items, contact.Contact{PhoneNumber: number.PhoneNumber})
|
||||
}
|
||||
}
|
||||
default:
|
||||
return nil, fmt.Errorf("Unrecognized type")
|
||||
}
|
||||
|
||||
if len(items) == 0 {
|
||||
return nil, fmt.Errorf("Failure converting")
|
||||
} else {
|
||||
return items, nil
|
||||
}
|
||||
}
|
||||
default:
|
||||
return nil, fmt.Errorf("Invalid msgOrNumber")
|
||||
}
|
||||
return &msg, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user