File tree Expand file tree Collapse file tree 2 files changed +22
-8
lines changed
Expand file tree Collapse file tree 2 files changed +22
-8
lines changed Original file line number Diff line number Diff line change 11package analytics
22
3- func getString (msg map [string ]interface {}, field string ) string {
4- val , _ := msg [field ].(string )
5- return val
3+ type FieldGetter interface {
4+ GetField (field string ) (interface {}, bool )
65}
76
8- func ValidateFields (msg map [string ]interface {}) error {
9- if typ , ok := msg ["type" ].(string ); ok {
10- switch typ {
7+ func getString (msg FieldGetter , field string ) string {
8+ if val , ok := msg .GetField (field ); ok {
9+ if str , ok := val .(string ); ok {
10+ return str
11+ }
12+ }
13+ return ""
14+ }
15+
16+ func ValidateFields (msg FieldGetter ) error {
17+ typ , _ := msg .GetField ("type" )
18+ if str , ok := typ .(string ); ok {
19+ switch str {
1120 case "alias" :
1221 return Alias {
1322 Type : "alias" ,
@@ -51,6 +60,6 @@ func ValidateFields(msg map[string]interface{}) error {
5160 return FieldError {
5261 Type : "analytics.Event" ,
5362 Name : "Type" ,
54- Value : msg [ "type" ] ,
63+ Value : typ ,
5564 }
5665}
Original file line number Diff line number Diff line change @@ -6,13 +6,19 @@ import (
66)
77
88var _ Message = (Event )(nil )
9+ var _ FieldGetter = (Event )(nil )
910
1011type Event map [string ]interface {}
1112
1213func (e Event ) Validate () error {
1314 return ValidateFields (e )
1415}
1516
17+ func (e Event ) GetField (field string ) (val interface {}, ok bool ) {
18+ val , ok = e [field ]
19+ return
20+ }
21+
1622func TestValidateFieldsMissingType (t * testing.T ) {
1723 msg := Event {
1824 "userId" : "user123" ,
@@ -73,7 +79,6 @@ func TestValidateFieldsAliasInvalid(t *testing.T) {
7379 t .Error ("validating an invalid generic message succeeded:" , msg )
7480 } else if e , ok := err .(FieldError ); ! ok {
7581 t .Error ("invalid error type returned when validating a generic message:" , err )
76-
7782 } else if e != (FieldError {
7883 Type : "analytics.Alias" ,
7984 Name : "PreviousId" ,
You can’t perform that action at this time.
0 commit comments