Skip to content

Commit

Permalink
Merge pull request #4 from SoulKa/feature/add-examples
Browse files Browse the repository at this point in the history
Move examples to root
  • Loading branch information
SoulKa authored Nov 28, 2023
2 parents 28df883 + 252d10e commit 7f23ecc
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions examples/example_test.go → example_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package examples
package golymorph_test

import (
"encoding/json"
"fmt"
"github.com/SoulKa/golymorph"
"github.com/mitchellh/mapstructure"
"reflect"
"testing"
)

func TestBasicPolymorphismFromJson(t *testing.T) {
// ExampleUnmarshalJSON demonstrates how to use the polymorpher to unmarshal a JSON into a struct with a polymorphic field.
func ExampleUnmarshalJSON() {

// get a JSON that contains a payload with a type field that determines the type of the payload
alertEventJson := `{ "timestamp": "2023-11-27T22:14:09+00:00", "payload": { "type": "alert", "message": "something is broken!" } }`
Expand Down Expand Up @@ -55,9 +55,15 @@ func TestBasicPolymorphismFromJson(t *testing.T) {
// continue to work with the event
fmt.Printf("event: %+v\n", event)
fmt.Printf("event payload: %T %+v\n", event.Payload, event.Payload.(AlertPayload))

// Output:
// event: {Timestamp:2023-11-27T22:14:09+00:00 Payload:{Type:alert Message:something is broken!}}
// event payload: golymorph_test.AlertPayload {Type:alert Message:something is broken!}
}

func TestBasicPolymorphismWithManualParsing(t *testing.T) {
// ExampleTypeMapPolymorphism_AssignTargetType demonstrates how to use the polymorpher to assign the
// type of a polymorphic field in an existing struct instance.
func ExampleTypeMapPolymorphism_AssignTargetType() {

// get a JSON that contains a payload with a type field that determines the type of the payload
alertEventJson := `{ "timestamp": "2023-11-27T22:14:09+00:00", "payload": { "type": "alert", "message": "something is broken!" } }`
Expand Down Expand Up @@ -85,7 +91,7 @@ func TestBasicPolymorphismWithManualParsing(t *testing.T) {
// parse the JSON into a map
var jsonMap map[string]any
if err := json.Unmarshal([]byte(alertEventJson), &jsonMap); err != nil {
t.Fatalf("error unmarshalling JSON: %s", err)
panic(fmt.Sprintf("error unmarshalling JSON: %s", err))
}

// create a polymorpher that assigns the type of the payload based on the type field
Expand All @@ -95,21 +101,25 @@ func TestBasicPolymorphismWithManualParsing(t *testing.T) {
WithDiscriminatorAt("type").
Build()
if err != nil {
t.Fatalf("error building polymorpher: %s", err)
panic(fmt.Sprintf("error building polymorpher: %s", err))
}

// create a new event
var event Event
if err := polymorpher.AssignTargetType(&jsonMap, &event); err != nil {
t.Fatalf("error assigning target type: %s", err)
panic(fmt.Sprintf("error assigning target type: %s", err))
}

// use mapstructure to unmarshal the payload into the event
if err := mapstructure.Decode(jsonMap, &event); err != nil {
t.Fatalf("error decoding JSON map: %s", err)
panic(fmt.Sprintf("error decoding JSON map: %s", err))
}

// continue to work with the event
fmt.Printf("event: %+v\n", event)
fmt.Printf("event payload: %T %+v\n", event.Payload, event.Payload.(AlertPayload))

// Output:
// event: {Timestamp:2023-11-27T22:14:09+00:00 Payload:{Type:alert Message:something is broken!}}
// event payload: golymorph_test.AlertPayload {Type:alert Message:something is broken!}
}

0 comments on commit 7f23ecc

Please sign in to comment.