Skip to content

Commit

Permalink
Bump version 5.4.0, add method to parse event object updates. Fix bug…
Browse files Browse the repository at this point in the history
… in parse invoice objects from events that had a empty array for metadata
  • Loading branch information
ppone committed Mar 8, 2020
1 parent 77b77f0 commit ed5b719
Show file tree
Hide file tree
Showing 4 changed files with 317 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -24,7 +24,7 @@ go get -u https://github.com/Invoiced/invoiced-go
- >= Go 1.11
## Version
5.3.0
5.4.0
```go
//Will print out the version.
invd.Version()
Expand Down
2 changes: 1 addition & 1 deletion connector.go
Expand Up @@ -17,7 +17,7 @@ const devRequestURL = "https://api.sandbox.invoiced.com"
const requestType = "application/json"
const InvoicedTokenString = "invoicedToken"

const version = "5.3.0"
const version = "5.4.0"

func Version() string {
return version
Expand Down
74 changes: 70 additions & 4 deletions invdendpoint/events.go
Expand Up @@ -3,6 +3,7 @@ package invdendpoint
import (
"encoding/json"
"errors"
"strings"
)

const EventsEndPoint = "/events"
Expand All @@ -18,9 +19,10 @@ type Event struct {

type EventObject struct {
Object *json.RawMessage `json:"object,omitempty"`
PreviousObject *json.RawMessage `json:"previous,omitempty"`
}

func (e *Event) ParseEventObject() (*json.RawMessage,error) {
func (e *Event) ParseEventObject() (*json.RawMessage, error) {
data := e.Data

eo := new(EventObject)
Expand All @@ -31,7 +33,7 @@ func (e *Event) ParseEventObject() (*json.RawMessage,error) {
return nil, err
}

err = json.Unmarshal(b,eo)
err = json.Unmarshal(b, eo)

if err != nil {
return nil, err
Expand All @@ -41,7 +43,32 @@ func (e *Event) ParseEventObject() (*json.RawMessage,error) {
return nil, errors.New("Could not parse event object")
}

return eo.Object,nil
return eo.Object, nil

}

func (e *Event) ParseEventPreviousObject() (*json.RawMessage, error) {
data := e.Data

eo := new(EventObject)

b, err := data.MarshalJSON()

if err != nil {
return nil, err
}

err = json.Unmarshal(b, eo)

if err != nil {
return nil, err
}

if eo.Object == nil {
return nil, errors.New("Could not parse event object")
}

return eo.PreviousObject, nil

}

Expand All @@ -55,18 +82,57 @@ func (e *Event) ParseInvoiceEvent() (*Invoice,error) {

b, err := eoData.MarshalJSON()

if err != nil {
return nil, err
}

bClean := CleanMetaDataArray(b)

ie := new(Invoice)

err = json.Unmarshal(bClean,ie)

if err != nil {
return nil, err
}

return ie, nil
}

func (e *Event) ParseInvoicePreviousEvent() (*Invoice,error) {
eoData, err := e.ParseEventPreviousObject()

if err != nil {
return nil, err
}

if eoData == nil {
return nil, err
}

b, err := eoData.MarshalJSON()


if err != nil {
return nil, err
}

bClean := CleanMetaDataArray(b)

ie := new(Invoice)

err = json.Unmarshal(b,ie)
err = json.Unmarshal(bClean,ie)

if err != nil {
return nil, err
}

return ie, nil
}

func CleanMetaDataArray(b []byte)[]byte {
s := string(b)
s1 := strings.Replace(s,`"metadata": []`,` "metadata": null`,-1)
s1 = strings.Replace(s1,`"metadata":[]`,` "metadata": null`,-1)
return []byte(s1)
}

0 comments on commit ed5b719

Please sign in to comment.