diff --git a/field_map.go b/field_map.go index 0a95f2b01..d0690b5f0 100644 --- a/field_map.go +++ b/field_map.go @@ -113,6 +113,16 @@ func (m FieldMap) SetField(tag Tag, field FieldValueWriter) FieldMap { return m } +//SetInt is a SetField wrapper for int fields +func (m FieldMap) SetInt(tag Tag, value int) FieldMap { + return m.SetField(tag, FIXInt(value)) +} + +//SetString is a SetField wrapper for string fields +func (m FieldMap) SetString(tag Tag, value string) FieldMap { + return m.SetField(tag, FIXString(value)) +} + //Clear purges all fields from field map func (m *FieldMap) Clear() { m.tagLookup = make(map[Tag]TagValues) diff --git a/field_map_test.go b/field_map_test.go index 2f73e3b58..c7404eb60 100644 --- a/field_map_test.go +++ b/field_map_test.go @@ -84,3 +84,37 @@ func TestFieldMap_Total(t *testing.T) { t.Error("Total should includes all fields but checkSum- got ", fMap.total()) } } + +func TestFieldMap_TypedSetAndGet(t *testing.T) { + var fMap FieldMap + fMap.init() + + fMap.SetString(1, "hello") + fMap.SetInt(2, 256) + + s, err := fMap.GetString(1) + if err != nil { + t.Error("Unexpected Error", err) + } else if s != "hello" { + t.Errorf("Expected %v got %v", "hello", s) + } + + i, err := fMap.GetInt(2) + if err != nil { + t.Error("Unexpected Error", err) + } else if i != 256 { + t.Errorf("Expected %v got %v", 256, i) + } + + _, err = fMap.GetInt(1) + if err == nil { + t.Error("Type mismatch should occur error but nil") + } + + s, err = fMap.GetString(2) + if err != nil { + t.Error("Type mismatch should occur error but nil") + } else if s != "256" { + t.Errorf("Expected %v got %v", "256", s) + } +}