Created
October 5, 2014 04:36
-
-
Save theacodes/595ffbfa9beba13cf97b to your computer and use it in GitHub Desktop.
Generic protorpc containers
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
function flatten_generic_value(val){ | |
if(val.type === 'container'){ | |
return flatten_generic(val['container_value']); | |
} else if(val.type === 'list'){ | |
return val.list_value.map(flatten_generic_value); | |
} else { | |
return val[val.type + '_value']; | |
} | |
}; | |
function flatten_generic(container){ | |
var result = {}; | |
container.values.forEach(function(val){ | |
result[val.key] = flatten_generic_value(val); | |
}); | |
return result; | |
} | |
/* Usage */ | |
flatten_generic(response); |
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
import ferris3 | |
import endpoints | |
from protorpc import messages | |
import types | |
class GenericValueMessage(messages.Message): | |
key = messages.StringField(1, required=True) | |
type = messages.StringField(2, required=True) | |
string_value = messages.StringField(4) | |
int_value = messages.IntegerField(5) | |
float_value = messages.FloatField(6) | |
bool_value = messages.BooleanField(7) | |
container_value = messages.MessageField('GenericContainerMessage', 8) | |
list_value = messages.MessageField('GenericValueMessage', 9, repeated=True) | |
class GenericContainerMessage(messages.Message): | |
values = messages.MessageField(GenericValueMessage, 1, repeated=True) | |
def value_to_message(key, value): | |
value_msg = GenericValueMessage(key=key) | |
if isinstance(value, types.StringTypes): | |
value_msg.type = 'string' | |
value_msg.string_value = value | |
elif isinstance(value, int): | |
value_msg.type = 'int' | |
value_msg.int_value = value | |
elif isinstance(value, float): | |
value_msg.type = 'float' | |
value_msg.float_value = value | |
elif isinstance(value, bool): | |
value_msg.type = 'bool' | |
value_msg.bool_value = value | |
elif isinstance(value, dict): | |
value_msg.type = 'container' | |
value_msg.container_value = dict_to_message(value) | |
elif isinstance(value, (list, tuple)): | |
value_msg.type = 'list' | |
value_msg.list_value = [value_to_message(str(n), x) for n, x in enumerate(value)] | |
else: | |
value_msg.type = 'unknown' | |
value_msg.string_value = str(value) | |
return value_msg | |
def dict_to_message(data): | |
container_msg = GenericContainerMessage() | |
for key, value in data.iteritems(): | |
value_msg = value_to_message(key, value) | |
container_msg.values.append(value_msg) | |
return container_msg | |
data = { | |
'a_test_string': 'hi', | |
'a_test_int': 4, | |
'a_test_floatt': 3.14, | |
'a_test_bool': True, | |
'a_test_container': { | |
'name': 'Jon', | |
'email': 'jaun@parrott.com' | |
}, | |
'a_test_array': [1, 'blue', True] | |
} | |
@ferris3.auto_service | |
class GenericService(ferris3.Service): | |
@ferris3.auto_method(returns=GenericContainerMessage) | |
# The weird syntax on the request method is an annotation. We're telling | |
# Ferris that we want the request to be of the type "GuestbookMessage". | |
def test(self, request): | |
return dict_to_message(data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment