| 1 |
package plist |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding" |
| 5 |
"reflect" |
| 6 |
"time" |
| 7 |
) |
| 8 |
|
| 9 |
func isEmptyValue(v reflect.Value) bool { |
| 10 |
switch v.Kind() { |
| 11 |
case reflect.Array, reflect.Map, reflect.Slice, reflect.String: |
| 12 |
return v.Len() == 0 |
| 13 |
case reflect.Bool: |
| 14 |
return !v.Bool() |
| 15 |
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: |
| 16 |
return v.Int() == 0 |
| 17 |
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: |
| 18 |
return v.Uint() == 0 |
| 19 |
case reflect.Float32, reflect.Float64: |
| 20 |
return v.Float() == 0 |
| 21 |
case reflect.Interface, reflect.Ptr: |
| 22 |
return v.IsNil() |
| 23 |
} |
| 24 |
return false |
| 25 |
} |
| 26 |
|
| 27 |
var ( |
| 28 |
plistMarshalerType = reflect.TypeOf((*Marshaler)(nil)).Elem() |
| 29 |
textMarshalerType = reflect.TypeOf((*encoding.TextMarshaler)(nil)).Elem() |
| 30 |
timeType = reflect.TypeOf((*time.Time)(nil)).Elem() |
| 31 |
) |
| 32 |
|
| 33 |
func implementsInterface(val reflect.Value, interfaceType reflect.Type) (interface{}, bool) { |
| 34 |
if val.CanInterface() { |
| 35 |
itf := val.Interface() |
| 36 |
if itf != nil && reflect.TypeOf(itf).Implements(interfaceType) { |
| 37 |
return itf, true |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
if val.CanAddr() { |
| 42 |
if pv := val.Addr(); pv.CanInterface() { |
| 43 |
itf := pv.Interface() |
| 44 |
if itf != nil && reflect.TypeOf(itf).Implements(interfaceType) { |
| 45 |
return itf, true |
| 46 |
} |
| 47 |
} |
| 48 |
} |
| 49 |
return nil, false |
| 50 |
} |
| 51 |
|
| 52 |
func (p *Encoder) marshalPlistInterface(marshalable Marshaler) cfValue { |
| 53 |
value, err := marshalable.MarshalPlist() |
| 54 |
if err != nil { |
| 55 |
panic(err) |
| 56 |
} |
| 57 |
return p.marshal(reflect.ValueOf(value)) |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
func (p *Encoder) marshalTextInterface(marshalable encoding.TextMarshaler) cfValue { |
| 62 |
s, err := marshalable.MarshalText() |
| 63 |
if err != nil { |
| 64 |
panic(err) |
| 65 |
} |
| 66 |
return cfString(s) |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
func (p *Encoder) marshalStruct(typ reflect.Type, val reflect.Value) cfValue { |
| 71 |
tinfo, _ := getTypeInfo(typ) |
| 72 |
|
| 73 |
dict := &cfDictionary{ |
| 74 |
keys: make([]string, 0, len(tinfo.fields)), |
| 75 |
values: make([]cfValue, 0, len(tinfo.fields)), |
| 76 |
} |
| 77 |
for _, finfo := range tinfo.fields { |
| 78 |
value := finfo.value(val) |
| 79 |
if !value.IsValid() || finfo.omitEmpty && isEmptyValue(value) { |
| 80 |
continue |
| 81 |
} |
| 82 |
dict.keys = append(dict.keys, finfo.name) |
| 83 |
dict.values = append(dict.values, p.marshal(value)) |
| 84 |
} |
| 85 |
|
| 86 |
return dict |
| 87 |
} |
| 88 |
|
| 89 |
func (p *Encoder) marshalTime(val reflect.Value) cfValue { |
| 90 |
time := val.Interface().(time.Time) |
| 91 |
return cfDate(time) |
| 92 |
} |
| 93 |
|
| 94 |
func (p *Encoder) marshal(val reflect.Value) cfValue { |
| 95 |
if !val.IsValid() { |
| 96 |
return nil |
| 97 |
} |
| 98 |
|
| 99 |
if receiver, can := implementsInterface(val, plistMarshalerType); can { |
| 100 |
return p.marshalPlistInterface(receiver.(Marshaler)) |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
if val.Type() == timeType { |
| 105 |
return p.marshalTime(val) |
| 106 |
} |
| 107 |
if val.Kind() == reflect.Ptr || (val.Kind() == reflect.Interface && val.NumMethod() == 0) { |
| 108 |
ival := val.Elem() |
| 109 |
if ival.IsValid() && ival.Type() == timeType { |
| 110 |
return p.marshalTime(ival) |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
if receiver, can := implementsInterface(val, textMarshalerType); can { |
| 116 |
return p.marshalTextInterface(receiver.(encoding.TextMarshaler)) |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
if val.Kind() == reflect.Ptr || (val.Kind() == reflect.Interface && val.NumMethod() == 0) { |
| 121 |
val = val.Elem() |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
if !val.IsValid() || ((val.Kind() == reflect.Ptr || val.Kind() == reflect.Interface) && val.IsNil()) { |
| 126 |
return nil |
| 127 |
} |
| 128 |
|
| 129 |
typ := val.Type() |
| 130 |
|
| 131 |
if typ == uidType { |
| 132 |
return cfUID(val.Uint()) |
| 133 |
} |
| 134 |
|
| 135 |
if val.Kind() == reflect.Struct { |
| 136 |
return p.marshalStruct(typ, val) |
| 137 |
} |
| 138 |
|
| 139 |
switch val.Kind() { |
| 140 |
case reflect.String: |
| 141 |
return cfString(val.String()) |
| 142 |
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: |
| 143 |
return &cfNumber{signed: true, value: uint64(val.Int())} |
| 144 |
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: |
| 145 |
return &cfNumber{signed: false, value: val.Uint()} |
| 146 |
case reflect.Float32: |
| 147 |
return &cfReal{wide: false, value: val.Float()} |
| 148 |
case reflect.Float64: |
| 149 |
return &cfReal{wide: true, value: val.Float()} |
| 150 |
case reflect.Bool: |
| 151 |
return cfBoolean(val.Bool()) |
| 152 |
case reflect.Slice, reflect.Array: |
| 153 |
if typ.Elem().Kind() == reflect.Uint8 { |
| 154 |
bytes := []byte(nil) |
| 155 |
if val.CanAddr() && val.Kind() == reflect.Slice { |
| 156 |
|
| 157 |
bytes = val.Bytes() |
| 158 |
} else { |
| 159 |
bytes = make([]byte, val.Len()) |
| 160 |
reflect.Copy(reflect.ValueOf(bytes), val) |
| 161 |
} |
| 162 |
return cfData(bytes) |
| 163 |
} else { |
| 164 |
values := make([]cfValue, val.Len()) |
| 165 |
for i, length := 0, val.Len(); i < length; i++ { |
| 166 |
if subpval := p.marshal(val.Index(i)); subpval != nil { |
| 167 |
values[i] = subpval |
| 168 |
} |
| 169 |
} |
| 170 |
return &cfArray{values} |
| 171 |
} |
| 172 |
case reflect.Map: |
| 173 |
if typ.Key().Kind() != reflect.String { |
| 174 |
panic(&unknownTypeError{typ}) |
| 175 |
} |
| 176 |
|
| 177 |
l := val.Len() |
| 178 |
dict := &cfDictionary{ |
| 179 |
keys: make([]string, 0, l), |
| 180 |
values: make([]cfValue, 0, l), |
| 181 |
} |
| 182 |
for _, keyv := range val.MapKeys() { |
| 183 |
if subpval := p.marshal(val.MapIndex(keyv)); subpval != nil { |
| 184 |
dict.keys = append(dict.keys, keyv.String()) |
| 185 |
dict.values = append(dict.values, subpval) |
| 186 |
} |
| 187 |
} |
| 188 |
return dict |
| 189 |
default: |
| 190 |
panic(&unknownTypeError{typ}) |
| 191 |
} |
| 192 |
} |