>>> I'm not sure we would add other types except for 'string' but I'm >>> inclined to something like the following. >> >> it looks like same as what i posted last week to me. > > The difference is that I don't think that we want to to put 'type' > into ofproto_* instead of a reference to the json encode/decode > implementation. ofproto_* explains 'types' and the code taking care of > encode/decode knows what to encode/decode for these types.
thanks for explanation. > >> are you happier with the following? > > I don't think that we need addrconv.plain_text stuff. JSON uses utf > for string. I don't think we need additional abstraction there. ok, i'll kill it. > >> or is tagging with a string ('string') essential? > > Not but as I explained above, I prefer ofproto_* to just define types. > > >> YAMAMOTO Takashi >> >> diff --git a/ryu/lib/stringify.py b/ryu/lib/stringify.py >> index b71da75..ea3b3b8 100644 >> --- a/ryu/lib/stringify.py >> +++ b/ryu/lib/stringify.py >> @@ -21,6 +21,9 @@ import collections >> import inspect >> >> >> +from ryu.lib import addrconv >> + >> + >> # Some arguments to __init__ is mungled in order to avoid name conflicts >> # with builtin names. >> # The standard mangling is to append '_' in order to avoid name clashes >> @@ -46,6 +49,9 @@ _mapdict_key = lambda f, d: dict([(f(k), v) for k, v in >> d.items()]) >> _mapdict_kv = lambda f, d: dict([(k, f(k, v)) for k, v in d.items()]) >> >> >> +StringType = addrconv.plain_text >> + >> + >> class StringifyMixin(object): >> _class_prefixes = [] >> >> @@ -80,7 +86,10 @@ class StringifyMixin(object): >> >> @classmethod >> def _get_converter(cls, k): >> - return cls._JSON_FORMATTER[k] >> + for t, attrs in cls._TYPE.iteritems(): >> + if k in attrs: >> + return t >> + raise KeyError >> >> @classmethod >> def _get_encoder(cls, k, encode_string): >> diff --git a/ryu/ofproto/ofproto_v1_0_parser.py >> b/ryu/ofproto/ofproto_v1_0_parser.py >> index d8e42bb..03c11ff 100644 >> --- a/ryu/ofproto/ofproto_v1_0_parser.py >> +++ b/ryu/ofproto/ofproto_v1_0_parser.py >> @@ -24,6 +24,7 @@ from . import ofproto_parser >> from . import ofproto_v1_0 >> from . import nx_match >> from ryu import utils >> +from ryu.lib.stringify import StringType >> >> import logging >> LOG = logging.getLogger('ryu.ofproto.ofproto_v1_0_parser') >> @@ -83,11 +84,13 @@ class OFPPhyPort(ofproto_parser.namedtuple('OFPPhyPort', >> ( >> 'port_no', 'hw_addr', 'name', 'config', 'state', 'curr', >> 'advertised', >> 'supported', 'peer'))): >> >> - _JSON_FORMATTER = { >> - 'hw_addr': addrconv.plain_text, >> - # XXX OF spec is unclear about the encoding of name. >> - # OVS seems to use UTF-8. >> - # 'name': addrconv.plain_text, > > hw_addr is ascii. JSON uses utf by default. I don't think that there > is any uncleanness here. the comment explains why 'name' below is commented out. YAMAMOTO Takashi > >> + _TYPE = { >> + StringType: [ >> + 'hw_addr', >> + # XXX OF spec is unclear about the encoding of name. >> + # OVS seems to use UTF-8. >> + # 'name' >> + ] >> } >> >> @classmethod >> @@ -2126,7 +2129,11 @@ class OFPFlowMod(MsgBase): >> @_set_msg_type(ofproto_v1_0.OFPT_PORT_MOD) >> class OFPPortMod(MsgBase): >> >> - _JSON_FORMATTER = {'hw_addr': addrconv.plain_text} >> + _TYPE = { >> + StringType: [ >> + 'hw_addr', >> + ] >> + } >> >> def __init__(self, datapath, port_no, hw_addr, config, mask, advertise): >> super(OFPPortMod, self).__init__(datapath) >> >>> >>> = >>>>From b5ddaabfbb4ad5500d70fdd350f616ae357058f4 Mon Sep 17 00:00:00 2001 >>> From: FUJITA Tomonori <fujita.tomon...@lab.ntt.co.jp> >>> Date: Sat, 24 Aug 2013 08:54:16 +0900 >>> Subject: [PATCH 3/6] stop be64 encode/decode for strings in JSON >>> representation >>> >>> Our JSON handles binary and string in the same way (use be64 >>> always). Had better to handle string in human readable way (string is >>> supposed to be utf-8 encoded/decoded by default in JSON). >>> >>> Signed-off-by: FUJITA Tomonori <fujita.tomon...@lab.ntt.co.jp> >>> --- >>> ryu/lib/stringify.py | 15 +++++++++++++-- >>> 1 file changed, 13 insertions(+), 2 deletions(-) >>> >>> diff --git a/ryu/lib/stringify.py b/ryu/lib/stringify.py >>> index 8b755c5..050aa0a 100644 >>> --- a/ryu/lib/stringify.py >>> +++ b/ryu/lib/stringify.py >>> @@ -79,9 +79,18 @@ class StringifyMixin(object): >>> return False >>> >>> @classmethod >>> + def _is_string_type(cls, k): >>> + if hasattr(cls, '_TYPE'): >>> + if 'string' in cls._TYPE and k in cls._TYPE['string']: >>> + return True >>> + return False >>> + >>> + @classmethod >>> def _encode_value(cls, k, v, encode_string=base64.b64encode): >>> encode = lambda x: cls._encode_value(k, x, encode_string) >>> - if isinstance(v, (bytes, unicode)): >>> + if cls._is_string_type(k): >>> + json_value = v.encode('utf') >>> + elif isinstance(v, (bytes, unicode)): >>> json_value = encode_string(v) >>> elif isinstance(v, list): >>> json_value = map(encode, v) >>> @@ -124,7 +133,9 @@ class StringifyMixin(object): >>> @classmethod >>> def _decode_value(cls, k, json_value, decode_string=base64.b64decode): >>> decode = lambda x: cls._decode_value(k, x, decode_string) >>> - if isinstance(json_value, (bytes, unicode)): >>> + if cls._is_string_type(k): >>> + v = json_value >>> + elif isinstance(json_value, (bytes, unicode)): >>> v = decode_string(json_value) >>> elif isinstance(json_value, list): >>> v = map(decode, json_value) >>> -- >>> 1.7.12.4 (Apple Git-37) >>> >>> >>> >>> >>> >>> ------------------------------------------------------------------------------ >>> Introducing Performance Central, a new site from SourceForge and >>> AppDynamics. Performance Central is your source for news, insights, >>> analysis and resources for efficient Application Performance Management. >>> Visit us today! >>> http://pubads.g.doubleclick.net/gampad/clk?id=48897511&iu=/4140/ostg.clktrk >>> _______________________________________________ >>> Ryu-devel mailing list >>> Ryu-devel@lists.sourceforge.net >>> https://lists.sourceforge.net/lists/listinfo/ryu-devel >> >> ------------------------------------------------------------------------------ >> Introducing Performance Central, a new site from SourceForge and >> AppDynamics. Performance Central is your source for news, insights, >> analysis and resources for efficient Application Performance Management. >> Visit us today! >> http://pubads.g.doubleclick.net/gampad/clk?id=48897511&iu=/4140/ostg.clktrk >> _______________________________________________ >> Ryu-devel mailing list >> Ryu-devel@lists.sourceforge.net >> https://lists.sourceforge.net/lists/listinfo/ryu-devel > > ------------------------------------------------------------------------------ > Introducing Performance Central, a new site from SourceForge and > AppDynamics. Performance Central is your source for news, insights, > analysis and resources for efficient Application Performance Management. > Visit us today! > http://pubads.g.doubleclick.net/gampad/clk?id=48897511&iu=/4140/ostg.clktrk > _______________________________________________ > Ryu-devel mailing list > Ryu-devel@lists.sourceforge.net > https://lists.sourceforge.net/lists/listinfo/ryu-devel ------------------------------------------------------------------------------ Introducing Performance Central, a new site from SourceForge and AppDynamics. Performance Central is your source for news, insights, analysis and resources for efficient Application Performance Management. Visit us today! http://pubads.g.doubleclick.net/gampad/clk?id=48897511&iu=/4140/ostg.clktrk _______________________________________________ Ryu-devel mailing list Ryu-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/ryu-devel