Skip to content

Commit 720562d

Browse files
Merge pull request #7 from troylelandshields/parse-uuid
Parse uuid
2 parents 2227a47 + a68e5d5 commit 720562d

File tree

3 files changed

+35
-12
lines changed

3 files changed

+35
-12
lines changed

app/scripts/client.controller.js

+23-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
'use strict';
22

3+
const uuidParse = require('uuid-parse');
4+
const uuidValidate = require('uuid-validate');
5+
36
angular
47
.module('app')
58
.controller('ClientController', ClientController);
@@ -24,9 +27,13 @@ function ClientController (GrpcSvc, $stateParams, $scope) {
2427
var json = {};
2528

2629
if (field.type && field.type == "bytes") {
27-
transformers[field.name] = function(str){
28-
var buffer = new Buffer(str, "utf-8");
29-
return buffer;
30+
transformers[field.name] = function(str){
31+
if (uuidValidate(str)) {
32+
var parsedUUID = uuidParse.parse(str);
33+
return new Buffer(parsedUUID);
34+
}
35+
36+
return new Buffer(str, "base64");
3037
}
3138
return "";
3239
}
@@ -77,7 +84,16 @@ function ClientController (GrpcSvc, $stateParams, $scope) {
7784
var transform = function(obj) {
7885
Object.keys(obj).forEach(function(key) {
7986
if (obj[key] instanceof Buffer) {
80-
obj[key] = obj[key].toString("utf-8");
87+
// try to convert to UUID. If your data was exactly 16 bytes and not a UUID, uhm... sorry :/
88+
if (obj[key].byteLength == 16) {
89+
var parsed = uuidParse.unparse(obj[key]);
90+
if (uuidValidate(parsed)) {
91+
obj[key] = parsed;
92+
return
93+
}
94+
}
95+
96+
obj[key] = obj[key].toString("base64");
8197
}
8298
if (typeof obj[key] == "object") {
8399
transform(obj[key]);
@@ -104,6 +120,9 @@ function ClientController (GrpcSvc, $stateParams, $scope) {
104120

105121
var transform = function(obj) {
106122
Object.keys(obj).forEach(function(key) {
123+
if (typeof obj != "object") {
124+
return
125+
}
107126
if (transformers[key]) {
108127
obj[key] = transformers[key](obj[key]);
109128
}

exampleSvc/main.go

+9-7
Original file line numberDiff line numberDiff line change
@@ -88,16 +88,18 @@ func (svc *Svc) SayHelloStream(srv ExampleService_SayHelloStreamServer) error {
8888
func (svc *Svc) Bytes(ctx context.Context, req *MessageWithBytes) (*MessageWithBytes, error) {
8989
fmt.Printf("bytes came in %s\n", string(req.Bytes))
9090

91-
id, err := uuid.New(req.Bytes)
92-
if err == nil {
93-
fmt.Printf("it is a UUID: %s\n", id.String())
94-
return &MessageWithBytes{
95-
Bytes: id.Bytes(),
96-
}, nil
91+
if len(req.Bytes) == 16 {
92+
id, err := uuid.New(req.Bytes)
93+
if err == nil {
94+
fmt.Printf("it is a UUID: %s\n", id.String())
95+
return &MessageWithBytes{
96+
Bytes: id.Bytes(),
97+
}, nil
98+
}
9799
}
98100

99101
var x interface{}
100-
err = json.Unmarshal(req.Bytes, &x)
102+
err := json.Unmarshal(req.Bytes, &x)
101103
if err == nil {
102104
fmt.Printf("it is JSON: %+v\n", x)
103105
return &MessageWithBytes{

package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
"dropzone": "^4.3.0",
2828
"jquery": "^2.1.4",
2929
"ngdropzone": "^2.0.1",
30-
"path": "^0.12.7"
30+
"path": "^0.12.7",
31+
"uuid-parse": "^1.0.0",
32+
"uuid-validate": "0.0.2"
3133
}
3234
}

0 commit comments

Comments
 (0)