Skip to content

Commit

Permalink
Set examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
hypirion committed Feb 6, 2016
1 parent 46e1f94 commit 470b0ab
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 7 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

[![GoDoc](https://godoc.org/gopkg.in/edn.v1?status.svg)](https://godoc.org/gopkg.in/edn.v1)

go-edn is a Golang library to read and write EDN (extensible data notation), a
subset of Clojure used for transferring data between applications, much like
JSON or XML. EDN is also a very good language for configuration files, much like
a JSON-like version of YAML.
go-edn is a Golang library to read and write
[EDN](https://github.com/edn-format/edn) (extensible data notation), a subset of
Clojure used for transferring data between applications, much like JSON or XML.
EDN is also a very good language for configuration files, much like a JSON-like
version of YAML.

This library is heavily influenced by the JSON library that ships with Go, and
people familiar with that package should know the basics of how this library
Expand Down
8 changes: 5 additions & 3 deletions edn_tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ var errorType = reflect.TypeOf((*error)(nil)).Elem()

// AddTagFn adds fn as a converter function for tagname tags to this TagMap. fn
// must have the signature func(T) (U, error), where T is the expected input
// type and U is the output type.
// type and U is the output type. See Decoder.AddTagFn for examples.
func (tm *TagMap) AddTagFn(tagname string, fn interface{}) error {
// TODO: check name
rfn := reflect.ValueOf(fn)
Expand Down Expand Up @@ -75,13 +75,15 @@ func (tm *TagMap) addVal(name string, val reflect.Value) error {

// AddTagFn adds fn as a converter function for tagname tags to the global
// TagMap. fn must have the signature func(T) (U, error), where T is the
// expected input type and U is the output type.
// expected input type and U is the output type. See Decoder.AddTagFn for
// examples.
func AddTagFn(tagname string, fn interface{}) error {
return globalTags.AddTagFn(tagname, fn)
}

// AddTagStructs adds the struct as a matching struct for tagname tags to this
// TagMap. val can not be a channel, function, interface or an unsafe pointer.
// See Decoder.AddTagStruct for examples.
func (tm *TagMap) AddTagStruct(tagname string, val interface{}) error {
rstruct := reflect.ValueOf(val)
switch rstruct.Type().Kind() {
Expand All @@ -93,7 +95,7 @@ func (tm *TagMap) AddTagStruct(tagname string, val interface{}) error {

// AddTagStructs adds the struct as a matching struct for tagname tags to the
// global TagMap. val can not be a channel, function, interface or an unsafe
// pointer.
// pointer. See Decoder.AddTagStruct for examples.
func AddTagStruct(tagname string, val interface{}) error {
return globalTags.AddTagStruct(tagname, val)
}
Expand Down
57 changes: 57 additions & 0 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,3 +222,60 @@ func ExampleTag_reading() {
fmt.Printf("Tag with name %s and value %q of type %T\n", tag.Tagname, tag.Value, tag.Value)
// Output: Tag with name unknown and value "???" of type edn.Symbol
}

// TODO: Lots and lots of unmarshalstring samples, refer to them in unmarshal

func ExampleUnmarshal_set() {
// map[T]bool is considered as sets as well as maps
var val map[int]bool

edn.UnmarshalString("#{1 -5 42}", &val)
fmt.Println(val[42], val[123]) // => true false

edn.UnmarshalString("{1 false 2 true}", &val)
fmt.Println(val[1], val[2]) // => false true

// Output:
// true false
// false true
}

func ExampleMarshal_set() {
// values of type map[T]bool and map[T]struct{} are encoded as EDN sets by
// default
val := map[int]bool{42: true}

bs, _ := edn.Marshal(val)
fmt.Println(string(bs)) // => #{42}

val2 := map[string]struct{}{"hiccup": {}}
bs, _ = edn.Marshal(val2)
fmt.Println(string(bs)) // => #{"hiccup"}

// Output:
// #{42}
// #{"hiccup"}
}

func ExampleMarshal_setOverride() {
// You can specify that map[T]bool/struct{} are printed as EDN maps by using
// the `map` keyword in the EDN struct tag:
type Value struct {
BoolMap map[int]bool `edn:"bool-map,map,omitempty"`
StructMap map[string]struct{} `edn:"struct-map,map,omitempty"`
}

var val Value
val.BoolMap = map[int]bool{2: false}
bs, _ := edn.Marshal(val)
fmt.Println(string(bs)) // => {:bool-map{2 false}}

val.BoolMap = nil
val.StructMap = map[string]struct{}{"foo": {}}
bs, _ = edn.Marshal(val)
fmt.Println(string(bs)) // => {:struct-map{"foo"{}}}

// Output:
// {:bool-map{2 false}}
// {:struct-map{"foo"{}}}
}

0 comments on commit 470b0ab

Please sign in to comment.