From 470b0ab438bbcd34ad4b5cab3d1eb1c13d69d6b9 Mon Sep 17 00:00:00 2001 From: Jean Niklas L'orange Date: Sat, 6 Feb 2016 23:04:05 +0100 Subject: [PATCH] Set examples. --- README.md | 9 ++++---- edn_tags.go | 8 ++++--- example_test.go | 57 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index be9312b..79a1b31 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/edn_tags.go b/edn_tags.go index 1623af2..d10e65f 100644 --- a/edn_tags.go +++ b/edn_tags.go @@ -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) @@ -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() { @@ -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) } diff --git a/example_test.go b/example_test.go index 3129daf..37b7978 100644 --- a/example_test.go +++ b/example_test.go @@ -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"{}}} +}