• Stars
    star
    9
  • Rank 1,939,727 (Top 39 %)
  • Language
    Go
  • License
    Apache License 2.0
  • Created over 6 years ago
  • Updated about 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Go package provides a generic interface to encoders and decoders

GoDoc codecov Build Status Go Report Card License Mentioned in Awesome Go

encoding

Package provides a generic interface to encoders and decoders

Example

package main
  
import (
        "flag"
        "fmt"
        "log"
        "strings"

        "github.com/mickep76/encoding"
        _ "github.com/mickep76/encoding/json"
        _ "github.com/mickep76/encoding/toml"
        _ "github.com/mickep76/encoding/yaml"
)

type Message struct {
        Name, Text string
}

type Messages struct {
        Messages []*Message
}

func main() {
        codec := flag.String("codec", "json", fmt.Sprintf("Codecs: [%s].", strings.Join(encoding.Codecs(), ", ")))
        indent := flag.String("indent", "", "Indent encoding (only supported by JSON codec)")
        flag.Parse()

        in := Messages{
                Messages: []*Message{
                        &Message{Name: "Ed", Text: "Knock knock."},
                        &Message{Name: "Sam", Text: "Who's there?"},
                        &Message{Name: "Ed", Text: "Go fmt."},
                        &Message{Name: "Sam", Text: "Go fmt who?"},
                        &Message{Name: "Ed", Text: "Go fmt yourself!"},
                },
        }

        var opts []encoding.Option
        if *indent != "" {
                opts = append(opts, encoding.WithIndent(*indent))
        }
        c, err := encoding.NewCodec(*codec, opts...)
        if err != nil {
                log.Fatal(err)
        }

        b, err := c.Encode(in)
        if err != nil {
                log.Fatal(err)
        }

        fmt.Printf("Codec: %s\n", *codec)
        fmt.Printf("Encoded:\n%s\n", string(b))

        out := Messages{}
        if err := c.Decode(b, &out); err != nil {
                log.Fatal(err)
        }

        fmt.Println("Decoded:")
        for _, m := range out.Messages {
                fmt.Printf("%s: %s\n", m.Name, m.Text)
        }
}

More Repositories

1

etcdtool

Export/Import/Edit etcd directory as JSON/YAML/TOML and validate directory using JSON schema
Go
171
star
2

docker-build-ami

Build Amazon EC2 AMI image using a Dockerfile
Python
68
star
3

mapslice-json

Go MapSlice for ordered marshal/ unmarshal of maps in JSON
Go
19
star
4

pepa

Configuration templating for SaltStack using Hierarchical substitution and Jinja.
Python
14
star
5

tf

Template files in Bash using YAML/JSON/TOML or Etcd input and GO text template
Go
10
star
6

distill

Template engine using hierarchical substitution for Puppet through the ENC interface
Perl
7
star
7

etcdmap

Go package provides methods for interacting with Etcd using struct, map or JSON
Go
7
star
8

netlink

Netlink Go package for listening to interface events and getting extended flags such as IFF_RUNNING.
Go
6
star
9

go-sff

Go decoder for SFP/QSFP Eeprom modules based on SFF-8079 and SFF-8636
Go
5
star
10

peekaboo

Micro-service for exposing system and hardware information
Go
4
star
11

compress

Package provides a generic interface to compress and un-compress
Go
4
star
12

grpc-exec-example

gRPC remote exec example
Go
3
star
13

osquery-protobuf

osquery database schemas converted to protobuf
Go
3
star
14

jsonptr

Go implementation of JSON pointer
Go
2
star
15

auth

Package provides JWT using public keys and a generic interface to authentication such as LDAP
Go
2
star
16

go-workday

Workday client written in Go
Go
2
star
17

typeconv

Provides conversion from a string to specific Go type
Go
2
star
18

typecomp

Provides comparison for different Go types and a Comparer interface when using structs
Go
1
star