Created
January 18, 2018 10:56
-
-
Save codingconcepts/a6e25668971ef0e9d5b06fe6bb713d77 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
"log" | |
"strings" | |
) | |
func main() { | |
if err := eventReceived("x-one", []byte(`{ "name": "Dan" }`)); err != nil { | |
log.Fatalf("error on event received: %v", err) | |
} | |
if err := eventReceived("x-two", []byte(`{ "age": 21 }`)); err != nil { | |
log.Fatalf("error on event received: %v", err) | |
} | |
} | |
func eventReceived(eventType string, eventJSON []byte) (err error) { | |
switch strings.ToLower(eventType) { | |
case "x-one": | |
target := one{} | |
if err = json.Unmarshal(eventJSON, &target); err != nil { | |
return | |
} | |
processOne(target) | |
case "x-two": | |
target := two{} | |
if err = json.Unmarshal(eventJSON, &target); err != nil { | |
return | |
} | |
processTwo(target) | |
default: | |
return fmt.Errorf("%q is not a valid event type", eventType) | |
} | |
return | |
} | |
func processOne(o one) { | |
fmt.Printf("PROCESSING: %v\n", o) | |
} | |
func processTwo(t two) { | |
fmt.Printf("PROCESSING: %v\n", t) | |
} | |
type one struct { | |
Name string `json:"name"` | |
} | |
type two struct { | |
Age int `json:"age"` | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment