diff --git a/kvstore.go b/kvstore.go index 2c35940..4eafe7c 100644 --- a/kvstore.go +++ b/kvstore.go @@ -3,6 +3,7 @@ package main import ( "reflect" + "github.com/ugorji/go/codec" "samhofi.us/x/keybase/types/chat1" ) @@ -63,3 +64,25 @@ func getTypeName(v interface{}) string { } return t.Name() } + +func encodeStructToJSONString(v interface{}) (string, error) { + jh := codecHandle() + var bytes []byte + err := codec.NewEncoderBytes(&bytes, jh).Encode(v) + if err != nil { + return "", err + } + result := string(bytes) + return result, nil +} + +func decodeJSONStringToStruct(v interface{}, src string) error { + bytes := []byte(src) + jh := codecHandle() + return codec.NewDecoderBytes(bytes, jh).Decode(v) +} + +func codecHandle() *codec.JsonHandle { + var jh codec.JsonHandle + return &jh +} diff --git a/utils.go b/utils.go index ef8ba5e..b401642 100644 --- a/utils.go +++ b/utils.go @@ -5,7 +5,6 @@ import ( "fmt" "github.com/teris-io/shortid" - "github.com/ugorji/go/codec" "samhofi.us/x/keybase/types/chat1" ) @@ -30,28 +29,6 @@ func getFeedbackExtendedDescription(bc botConfig) *chat1.UserBotExtendedDescript } } -func encodeStructToJSONString(v interface{}) (string, error) { - jh := codecHandle() - var bytes []byte - err := codec.NewEncoderBytes(&bytes, jh).Encode(v) - if err != nil { - return "", err - } - result := string(bytes) - return result, nil -} - -func decodeJSONStringToStruct(v interface{}, src string) error { - bytes := []byte(src) - jh := codecHandle() - return codec.NewDecoderBytes(bytes, jh).Decode(v) -} - -func codecHandle() *codec.JsonHandle { - var jh codec.JsonHandle - return &jh -} - func (b *bot) logError(err error) string { // generate the error id eid := shortid.MustGenerate()