Skip to content

Commit

Permalink
Add ADIFRecord.DeleteField()
Browse files Browse the repository at this point in the history
  • Loading branch information
jj1bdx committed Oct 12, 2022
1 parent d86d300 commit f6a6505
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
11 changes: 11 additions & 0 deletions adifrecord.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ type ADIFRecord interface {
SetValue(string, string)
// Get all of the present field names
GetFields() []string
// Delete a field
DeleteField(string) (bool, error)
}

// Internal implementation for ADIFRecord
Expand Down Expand Up @@ -99,3 +101,12 @@ func (r *baseADIFRecord) GetFields() []string {
}
return keys
}

// Delete a field (from the internal map)
func (r *baseADIFRecord) DeleteField(name string) (bool, error) {
if _, ok := r.values[name]; ok {
delete(r.values, name)
return true, nil
}
return false, ErrNoSuchField
}
36 changes: 36 additions & 0 deletions adifrecord_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,39 @@ OUTER:
t.Fatalf("Expected field %v wasn't in the actual fields", exp)
}
}

func TestDeleteField(t *testing.T) {
testData := map[string]string{
"call": "W1AW",
"notes": "This is a notice",
"STATION_CALL": "KF4MDV"}
expected := [2]string{"call", "station_call"}

record := NewADIFRecord()
for k, v := range testData {
record.SetValue(k, v)
}
success, err := record.DeleteField("nothere")
if err != ErrNoSuchField {
t.Fatal(err)
}
success, err = record.DeleteField("notes")
if !success || err != nil {
t.Fatalf("success: %t, err: %v", success, err)
}
fieldNames := record.GetFields()

if len(fieldNames) != len(expected) {
t.Fatalf("Expected %d fields but got %d", len(expected), len(fieldNames))
}

OUTER:
for _, exp := range expected {
for _, field := range fieldNames {
if exp == field {
continue OUTER
}
}
t.Fatalf("Expected field %v wasn't in the actual fields", exp)
}
}

0 comments on commit f6a6505

Please sign in to comment.