forked from micro/go-micro
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add configurable codecs for broker/transport
- Loading branch information
Asim Aslam
committed
Dec 6, 2016
1 parent
e102599
commit bd3c798
Showing
6 changed files
with
100 additions
and
3 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package broker | ||
|
||
import ( | ||
"encoding/json" | ||
) | ||
|
||
type jsonCodec struct{} | ||
|
||
func (j jsonCodec) Marshal(v interface{}) ([]byte, error) { | ||
return json.Marshal(v) | ||
} | ||
|
||
func (j jsonCodec) Unmarshal(d []byte, v interface{}) error { | ||
return json.Unmarshal(d, v) | ||
} | ||
|
||
func (j jsonCodec) String() string { | ||
return "json" | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package json | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"github.com/micro/go-micro/broker" | ||
) | ||
|
||
type jsonCodec struct{} | ||
|
||
func (j jsonCodec) Marshal(v interface{}) ([]byte, error) { | ||
return json.Marshal(v) | ||
} | ||
|
||
func (j jsonCodec) Unmarshal(d []byte, v interface{}) error { | ||
return json.Unmarshal(d, v) | ||
} | ||
|
||
func (j jsonCodec) String() string { | ||
return "json" | ||
} | ||
|
||
func NewCodec() broker.Codec { | ||
return jsonCodec{} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package noop | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/micro/go-micro/broker" | ||
) | ||
|
||
type noopCodec struct{} | ||
|
||
func (n noopCodec) Marshal(v interface{}) ([]byte, error) { | ||
msg, ok := v.(*broker.Message) | ||
if !ok { | ||
return nil, errors.New("invalid message") | ||
} | ||
return msg.Body, nil | ||
} | ||
|
||
func (n noopCodec) Unmarshal(d []byte, v interface{}) error { | ||
msg, ok := v.(*broker.Message) | ||
if !ok { | ||
return errors.New("invalid message") | ||
} | ||
msg.Body = d | ||
return nil | ||
} | ||
|
||
func (n noopCodec) String() string { | ||
return "noop" | ||
} | ||
|
||
func NewCodec() broker.Codec { | ||
return noopCodec{} | ||
} |
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
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