Skip to content

Commit

Permalink
Add address parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
charlieegan3 committed Sep 29, 2021
1 parent e91d30e commit 4b8c391
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/airtable/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func Download(client *air.Client, databaseID, tableName, viewName string) ([]map
result, err = table.GetRecords().
FromView(viewName).
WithOffset(offset).
ReturnFields("Display Name", "JSON Phone Numbers", "JSON Emails", "Note", "Company", "Profile Image", "Birthday", "JSON Special Days").
ReturnFields("Display Name", "JSON Addresses", "JSON Phone Numbers", "JSON Emails", "Note", "Company", "Profile Image", "Birthday", "JSON Special Days").
Do()
if err != nil {
return fmt.Errorf("failed to get records: %s", err)
Expand Down
13 changes: 13 additions & 0 deletions pkg/vcard/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,19 @@ func Generate(contacts []map[string]interface{}, useV3 bool, photoSize int, id s
}
}

// set addresses
if val, ok := contact["JSON Addresses"].(string); ok {
decoder := json.NewDecoder(strings.NewReader(val))
var addresses []govcard.Address
err := decoder.Decode(&addresses)
if err != nil {
return "", fmt.Errorf("failed to parse JSON addresses: %s", err)
}
for _, address := range addresses {
card.AddAddress(&address)
}
}

// set note
if val, ok := contact["Note"].(string); ok {
card.SetValue(govcard.FieldNote, val)
Expand Down
18 changes: 18 additions & 0 deletions pkg/vcard/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,24 @@ VERSION:4.0
FN:John Appleseed
N:Appleseed;John;;;
UID:example
END:VCARD`,
},
{
Description: "it sets addresses correctly",
Fields: []map[string]interface{}{
{
"Display Name": "John Appleseed",
"JSON Addresses": `[
{"ExtendedAddress":"","StreetAddress":"15 Whitby Road","Region":"London","PostalCode":"IV65NE","Country":"UK"},
{"ExtendedAddress":"Flat 2","StreetAddress":"Sweden Road","Region":"Vaxjo","PostalCode":"12345","Country":"Sweden"}]`,
},
},
ExpectedOutput: `BEGIN:VCARD
VERSION:4.0
ADR:;;15 Whitby Road;;London;IV65NE;UK
ADR:;Flat 2;Sweden Road;;Vaxjo;12345;Sweden
FN:John Appleseed
N:Appleseed;John;;;
END:VCARD`,
},
}
Expand Down

0 comments on commit 4b8c391

Please sign in to comment.