Add API Framework Revel Source Files
[iec.git] / src / foundation / api / revel / cache / serialization.go
1 // Copyright (c) 2012-2016 The Revel Framework Authors, All rights reserved.
2 // Revel Framework source code and usage is governed by a MIT style
3 // license that can be found in the LICENSE file.
4
5 package cache
6
7 import (
8         "bytes"
9         "encoding/gob"
10         "reflect"
11         "strconv"
12 )
13
14 // Serialize transforms the given value into bytes following these rules:
15 //   - If value is a byte array, it is returned as-is.
16 //   - If value is an int or uint type, it is returned as the ASCII representation
17 //   - Else, encoding/gob is used to serialize
18 func Serialize(value interface{}) ([]byte, error) {
19         if data, ok := value.([]byte); ok {
20                 return data, nil
21         }
22
23         switch v := reflect.ValueOf(value); v.Kind() {
24         case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
25                 return []byte(strconv.FormatInt(v.Int(), 10)), nil
26         case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
27                 return []byte(strconv.FormatUint(v.Uint(), 10)), nil
28         }
29
30         var b bytes.Buffer
31         encoder := gob.NewEncoder(&b)
32         if err := encoder.Encode(value); err != nil {
33                 cacheLog.Error("Serialize: gob encoding failed", "value", value, "error", err)
34                 return nil, err
35         }
36         return b.Bytes(), nil
37 }
38
39 // Deserialize transforms bytes produced by Serialize back into a Go object,
40 // storing it into "ptr", which must be a pointer to the value type.
41 func Deserialize(byt []byte, ptr interface{}) (err error) {
42         if data, ok := ptr.(*[]byte); ok {
43                 *data = byt
44                 return
45         }
46
47         if v := reflect.ValueOf(ptr); v.Kind() == reflect.Ptr {
48                 switch p := v.Elem(); p.Kind() {
49                 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
50                         var i int64
51                         i, err = strconv.ParseInt(string(byt), 10, 64)
52                         if err != nil {
53                                 cacheLog.Error("Deserialize: failed to parse int", "value", string(byt), "error", err)
54                         } else {
55                                 p.SetInt(i)
56                         }
57                         return
58
59                 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
60                         var i uint64
61                         i, err = strconv.ParseUint(string(byt), 10, 64)
62                         if err != nil {
63                                 cacheLog.Error("Deserialize: failed to parse uint", "value", string(byt), "error", err)
64                         } else {
65                                 p.SetUint(i)
66                         }
67                         return
68                 }
69         }
70
71         b := bytes.NewBuffer(byt)
72         decoder := gob.NewDecoder(b)
73         if err = decoder.Decode(ptr); err != nil {
74                 cacheLog.Error("Deserialize: glob decoding failed", "error", err)
75                 return
76         }
77         return
78 }