Skip to content

Commit de2cd3f

Browse files
committed
initial work
0 parents  commit de2cd3f

File tree

6 files changed

+150
-0
lines changed

6 files changed

+150
-0
lines changed

.gitignore

Whitespace-only changes.

AUTHORS

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
go-simple-serializer is maintained by Spatial Current, Inc.
2+
3+
Authors:
4+
5+
* Patrick Dufour (pjdufour)

CONTRIBUTING.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Contributing to go-simple-serializer
2+
3+
## Contributor License Agreement
4+
5+
Thank you for your interest in contributing. You will first need to agree to the license. Simply post the following paragraph, with "your name" and "your github account" substituted as needed, to the first issue [#1](https://github.com/spatialcurrent/go-simple-serializer/issues/1). Otherwise, you can email us [here](mailto:[email protected]?subject=CLA).
6+
7+
I, **< YOUR NAME > (< YOUR GITHUB ACCOUNT >)**, agree to the license terms. My contributions to this repo are granted to **Spatial Current, Inc.** under a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable license and/or copyright is transferred.
8+
9+
## Versioning
10+
11+
This library is still in alpha (0.x.x), so versioning is not semantic yet.
12+
13+
## Authors
14+
15+
See [AUTHORS](https://github.com/spatialcurrent/go-simple-serializer/blob/master/AUTHORS) for a list of contributors.

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2018 Spatial Current, Inc.
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# go-simple-serializer
2+
3+
# Description
4+
5+
**go-simple-serializer** is a simple library for serializing and deserializing objects.
6+
7+
# Contributing
8+
9+
[Spatial Current, Inc.](https://spatialcurrent.io) is currently accepting pull requests for this repository. We'd love to have your contributions! Please see [Contributing.md](https://github.com/spatialcurrent/go-simple-serializer/blob/master/CONTRIBUTING.md) for how to get started.
10+
11+
# License
12+
13+
This work is distributed under the **MIT License**. See **LICENSE** file.

simpleserializer/Serialize.go

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package simpleserializer
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
"reflect"
7+
"encoding/csv"
8+
"encoding/json"
9+
"strings"
10+
)
11+
12+
import (
13+
"gopkg.in/yaml.v2"
14+
"github.com/pkg/errors"
15+
"github.com/BurntSushi/toml"
16+
)
17+
18+
func Serialize(input interface{}, format string) (string, error) {
19+
output := ""
20+
21+
if format == "csv" {
22+
s := reflect.ValueOf(input)
23+
if s.Kind() != reflect.Slice {
24+
return "", errors.New("Input is not of kind slice.")
25+
}
26+
if s.Len() > 0 {
27+
first := s.Index(0).Type()
28+
if first.Kind() == reflect.Ptr {
29+
first = first.Elem()
30+
}
31+
rows := make([][]string, 0)
32+
header := make([]string, first.NumField())
33+
for i := 0; i < first.NumField(); i++ {
34+
if tag := first.Field(i).Tag.Get("csv"); len(tag) > 0 {
35+
header[i] = tag
36+
} else {
37+
header[i] = first.Field(i).Name
38+
}
39+
}
40+
for i := 0; i < s.Len(); i++ {
41+
rows = append(rows, make([]string, first.NumField()))
42+
for j := 0; j < len(header); j++ {
43+
ft := first.Field(j).Type
44+
switch {
45+
case ft == reflect.TypeOf(""):
46+
rows[i][j] = s.Index(i).Elem().Field(j).String()
47+
case ft == reflect.TypeOf([]string{}):
48+
slice_as_strings := s.Index(i).Elem().Field(j).Interface().([]string)
49+
rows[i][j] = strings.Join(slice_as_strings, ",")
50+
default:
51+
rows[i][j] = ""
52+
}
53+
}
54+
}
55+
buf := new(bytes.Buffer)
56+
w := csv.NewWriter(buf)
57+
w.Write(header)
58+
w.WriteAll(rows)
59+
output = buf.String()
60+
}
61+
} else if format == "json" {
62+
text, err := json.Marshal(input)
63+
if err != nil {
64+
return "", err
65+
}
66+
output = string(text)
67+
} else if format == "jsonl" {
68+
s := reflect.ValueOf(input)
69+
if s.Kind() != reflect.Slice {
70+
return "", errors.New("Input is not of kind slice.")
71+
}
72+
for i := 0; i < s.Len(); i++ {
73+
text, err := json.Marshal(s.Index(i).Interface())
74+
if err != nil {
75+
return "", err
76+
}
77+
output += string(text)
78+
if i < s.Len() -1 {
79+
output += "\n"
80+
}
81+
}
82+
} else if format == "toml" {
83+
buf := new(bytes.Buffer)
84+
if err := toml.NewEncoder(buf).Encode(input); err != nil {
85+
return "", errors.New(fmt.Sprintf("Error encoding TOML: %s", err))
86+
}
87+
output = buf.String()
88+
} else if format == "yaml" {
89+
y, err := yaml.Marshal(input)
90+
if err != nil {
91+
return "", err
92+
}
93+
output = string(y)
94+
}
95+
return output, nil
96+
}

0 commit comments

Comments
 (0)