import datetime

from tendrl.commons.etcdobj import fields


def to_etcdobj(cls_etcd, obj):
    for attr, value in vars(obj).iteritems():
        if value is None:
            continue
        if attr.startswith("_"):
            continue
        if attr in ["definition"]:
            continue
        if type(value) == list:
            value = "\n".join(value)
        setattr(cls_etcd, attr, to_etcd_field(attr, value))
    return cls_etcd


def to_etcd_field(name, value):
    type_to_etcd_fields_map = {dict: fields.DictField,
                               str: fields.StrField,
                               int: fields.IntField,
                               bool: fields.StrField,
                               unicode: fields.StrField,
                               datetime.datetime: fields.StrField}
    if type(value) == dict:
        return fields.DictField(name, value, {'str': 'str'})
    return type_to_etcd_fields_map[type(value)](name, value)

@@ -8,16 +8,28 @@
 datetime
+%0Aimport json
 %0A%0Afrom t
@@ -177,32 +177,34 @@
             
-continue
+value = %22%22
 %0A        if 
@@ -358,17 +358,18 @@
 e = 
-%22%5Cn%22.join
+json.dumps
 (val

Update utils.py