Skip to content

Commit

Permalink
Had to use easyjson ( json encoding ) for values. Gob wouldn't allow …
Browse files Browse the repository at this point in the history
…unknown structures.
  • Loading branch information
qrpike committed Mar 21, 2019
1 parent 675972f commit ff216fd
Show file tree
Hide file tree
Showing 7 changed files with 684 additions and 42 deletions.
21 changes: 7 additions & 14 deletions errand-routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ package main

import (
// "fmt"
"bytes"
"errors"
"net/http"
"encoding/gob"
gin "github.com/gin-gonic/gin"
badger "github.com/dgraph-io/badger"
)
Expand Down Expand Up @@ -111,7 +109,7 @@ func ( s *ErrandsServer ) failedErrand( c *gin.Context ){


type CompletedRequest struct {
Results string `json:"results"`
Results *gin.H `json:"results"`
}
func ( s *ErrandsServer ) completeErrand( c *gin.Context ){
var updatedErrand *Errand
Expand Down Expand Up @@ -268,21 +266,16 @@ func ( s *ErrandsServer ) UpdateErrandByID( id string, fn func( *Errand ) error
item, err := txn.Get([]byte( id )); if err != nil {
return err
}
err = item.Value(func(val []byte) error {
var bytesBuffer bytes.Buffer
dec := gob.NewDecoder( &bytesBuffer )
_, err := bytesBuffer.Write( val ); if err != nil {
err = item.Value(func(v []byte) error {
errand := &Errand{}
err := errand.UnmarshalJSON( v ); if err != nil {
return err
}
var errand Errand
err = dec.Decode( &errand ); if err != nil {
err = fn( errand ); if err != nil {
return err
}
err = fn( &errand ); if err != nil {
return err
}
updatedErrand = &errand
err = s.saveErrand( txn, &errand ); if err != nil {
updatedErrand = errand
err = s.saveErrand( txn, errand ); if err != nil {
return err
}
return nil
Expand Down
4 changes: 3 additions & 1 deletion errand.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (


var ErrandStatuses []string = []string{"inactive", "active", "failed", "completed"}
//easyjson:json
type Errand struct {

// General Attributes:
Expand All @@ -27,7 +28,7 @@ type Errand struct {
Data *gin.H `json:"data,omitempty"`
Created int64 `json:"created"`
Status string `json:"status,omitempty"`
Results string `json:"results,omitempty"`
Results *gin.H `json:"results,omitempty"`

// Internal attributes:
Progress float64 `json:"progress"`
Expand All @@ -40,6 +41,7 @@ type Errand struct {


var LogSeverities []string = []string{ "INFO", "WARNING", "ERROR" }
//easyjson:json
type Log struct {
Severity string `json:"severity" binding:"required"`
Message string `json:"message" binding:"required"`
Expand Down
34 changes: 11 additions & 23 deletions errands-routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ import (
"log"
"sort"
// "time"
"bytes"
"errors"
"net/http"
"encoding/gob"
"encoding/json"
gin "github.com/gin-gonic/gin"
badger "github.com/dgraph-io/badger"
Expand Down Expand Up @@ -103,12 +101,10 @@ func ( s *ErrandsServer ) saveErrand( txn *badger.Txn, errand *Errand ) error {
if !contains(ErrandStatuses, errand.Status) {
return errors.New("Invalid errand status state")
}
var bytesBuffer bytes.Buffer
enc := gob.NewEncoder(&bytesBuffer)
err := enc.Encode( errand ); if err != nil {
bytes, err := errand.MarshalJSON(); if err != nil {
return err
}
return txn.Set([]byte(errand.ID), bytesBuffer.Bytes())
return txn.Set([]byte(errand.ID), bytes)
}


Expand Down Expand Up @@ -237,23 +233,20 @@ func ( s *ErrandsServer ) processErrand( c *gin.Context ){
for it.Rewind(); it.Valid(); it.Next() {
item := it.Item()
err := item.Value(func( v []byte ) error {
var bytesBuffer bytes.Buffer
dec := gob.NewDecoder( &bytesBuffer )
_, err := bytesBuffer.Write( v ); if err != nil {
return err
}
var errand Errand
err = dec.Decode( &errand ); if err != nil {

errand := &Errand{}
err := errand.UnmarshalJSON( v ); if err != nil {
return err
}

if errand.Status != "inactive" {
return nil
}
if errand.Type != typeFilter {
return nil
}
// Add to list of errands we could possibly process:
errands = append( errands, &errand )
errands = append( errands, errand )
return nil
})
if err != nil {
Expand Down Expand Up @@ -323,17 +316,12 @@ func ( s *ErrandsServer ) GetErrandsBy( fn func ( *Errand ) bool ) ( []*Errand,
for it.Rewind(); it.Valid(); it.Next() {
item := it.Item()
err := item.Value(func( v []byte ) error {
var bytesBuffer bytes.Buffer
dec := gob.NewDecoder( &bytesBuffer )
_, err := bytesBuffer.Write( v ); if err != nil {
return err
}
var errand Errand
err = dec.Decode( &errand ); if err != nil {
errand := &Errand{}
err := errand.UnmarshalJSON( v ); if err != nil {
return err
}
if( fn( &errand ) ){
errands = append( errands, &errand )
if( fn( errand ) ){
errands = append( errands, errand )
}
return nil
})
Expand Down
2 changes: 1 addition & 1 deletion errands-server.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (




//easyjson:json
type Notification struct {
Event string `json:"event"`
Errand Errand `json:"errand,omitempty"`
Expand Down
8 changes: 5 additions & 3 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ import:
version: ^0.0.1
- package: github.com/kelseyhightower/envconfig
version: ^1.3.0
- package: github.com/mailru/easyjson
Loading

0 comments on commit ff216fd

Please sign in to comment.