Documentation ¶
Index ¶
- func Encode(writer io.Writer, src interface{}) error
- type ShortWrite
- type StringCaster
- type UnsupportedSource
- type Value
- func (receiver Value) CASPERValue() (string, error)
- func (receiver Value) Else(datum string) Value
- func (receiver Value) ElseUnwrap(datum string) string
- func (receiver Value) GoString() string
- func (receiver Value) Map(fn func(string) string) Value
- func (receiver *Value) Scan(src interface{}) error
- func (receiver Value) String() (string, error)
- func (receiver Value) Then(fn func(string) Value) Value
- func (receiver *Value) UnmarshalText(text []byte) error
- func (receiver Value) Unwrap() (string, bool)
- func (receiver Value) Value() (driver.Value, error)
- type Valuer
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Encode ¶
Encode encodes ‘src’ as a Casper file data format, and writes it to ‘writer’.
‘src’ can be either any of the following map types:
• map[string]casper.Valuer
• map[string]encoding.TextMarshaler
• map[string]fmt.StringCaster
• map[string]fmt.Stringer
• map[string][]byte
• map[string]string
• map[string]interface{}
And can also be a struct that the appropriate “casper” struct tag.
Example (MapStringBytes) ¶
var data map[string][]byte = map[string][]byte{ "apple": []byte("ONE"), "banana": []byte("TWO"), "cherry": []byte("THREE"), } var buffer bytes.Buffer var writer io.Writer = &buffer err := casper.Encode(writer, data) if nil != err { fmt.Printf("ERROR: %s\n", err) return } fmt.Print(buffer.String())
Output: apple: ONE; banana: TWO; cherry: THREE;
Example (MapStringInterface) ¶
var data map[string]interface{} = map[string]interface{}{ "apple": "one", "banana": "two", "cherry": "three", "http": map[string]interface{}{ "port": "8080", "log": map[string]interface{}{ "dest": "stdout", }, }, "x-notice": Measurement{3, "cm"}, } var buffer bytes.Buffer var writer io.Writer = &buffer err := casper.Encode(writer, data) if nil != err { fmt.Printf("ERROR: %s\n", err) return } fmt.Print(buffer.String())
Output: apple: one; banana: two; cherry: three; http { log { dest: stdout; } port: 8080; } x-notice: 3cm;
Example (MapStringString) ¶
var data map[string]string = map[string]string{ "apple": "one", "banana": "two", "cherry": "three", } var buffer bytes.Buffer var writer io.Writer = &buffer err := casper.Encode(writer, data) if nil != err { fmt.Printf("ERROR: %s\n", err) return } fmt.Print(buffer.String())
Output: apple: one; banana: two; cherry: three;
Example (MapStringStringCaster) ¶
package main import ( "github.com/reiver/go-casper" "bytes" "fmt" "io" "strings" ) type Title struct { value string } func (receiver Title) String() (string, error) { var s string = strings.Title(receiver.value) return s, nil } func main() { var data map[string]casper.StringCaster = map[string]casper.StringCaster{ "apple": Title{"one"}, "banana": Title{"two"}, "cherry": Title{"three"}, } var buffer bytes.Buffer var writer io.Writer = &buffer err := casper.Encode(writer, data) if nil != err { fmt.Printf("ERROR: %s\n", err) return } fmt.Print(buffer.String()) }
Output: apple: One; banana: Two; cherry: Three;
Example (MapStringStringer) ¶
package main import ( "github.com/reiver/go-casper" "bytes" "fmt" "io" ) type Optional struct { loaded bool value string } func (receiver Optional) String() string { if !receiver.loaded { return "Nothing()" } return fmt.Sprintf("Something(%q)", receiver.value) } func Nothing() Optional { return Optional{} } func Something(v string) Optional { return Optional{ loaded: true, value: v, } } func main() { var data map[string]fmt.Stringer = map[string]fmt.Stringer{ "apple": Something("red"), "banana": Something("yellow"), "cherry": Nothing(), } var buffer bytes.Buffer var writer io.Writer = &buffer err := casper.Encode(writer, data) if nil != err { fmt.Printf("ERROR: %s\n", err) return } fmt.Print(buffer.String()) }
Output: apple: Something("red"); banana: Something("yellow"); cherry: Nothing();
Example (MapStringTextMarshaler) ¶
package main import ( "github.com/reiver/go-casper" "bytes" "encoding" "fmt" "io" "strings" ) type Upper struct { value string } func (receiver Upper) MarshalText() ([]byte, error) { var s string = strings.ToUpper(receiver.value) return []byte(s), nil } func main() { var data map[string]encoding.TextMarshaler = map[string]encoding.TextMarshaler{ "apple": Upper{"one"}, "banana": Upper{"two"}, "cherry": Upper{"three"}, } var buffer bytes.Buffer var writer io.Writer = &buffer err := casper.Encode(writer, data) if nil != err { fmt.Printf("ERROR: %s\n", err) return } fmt.Print(buffer.String()) }
Output: apple: ONE; banana: TWO; cherry: THREE;
Example (MapStringValuer) ¶
package main import ( "github.com/reiver/go-casper" "bytes" "fmt" "io" ) type Measurement struct { value int unit string } func (receiver Measurement) CASPERValue() (string, error) { var buffer bytes.Buffer _, err := fmt.Fprintf(&buffer, "%d%s", receiver.value, receiver.unit) if nil != err { return "", err } return buffer.String(), nil } func main() { var data map[string]casper.Valuer = map[string]casper.Valuer{ "apple": Measurement{5, "cm"}, "banana": Measurement{7, "in"}, "cherry": Measurement{12, "lbs"}, } var buffer bytes.Buffer var writer io.Writer = &buffer err := casper.Encode(writer, data) if nil != err { fmt.Printf("ERROR: %s\n", err) return } fmt.Print(buffer.String()) }
Output: apple: 5cm; banana: 7in; cherry: 12lbs;
Types ¶
type ShortWrite ¶
type ShortWrite interface { error ShortWrite() // Returns what trying to be written. Source() string // Returns the number of bytes that were expected to be written. Expected() int64 // Returns the number of bytes that were actually written. Actual() int64 }
ShortWrite is the error returned from casper.Encode() when, for whatever reason, not everything was able to be written to an io.Writer.
For example:
err := casper.Encode(writer, source) if nil != err { switch err.(type) { case casper.ShortWrite: //@TODO default: //@TODO } }
type StringCaster ¶
type UnsupportedSource ¶
UnsupportedSource is the error returned from casper.Encode(), and casper.Value.Scan() when the type of the source is not supported.
For example:
var source int64 // <---- Note that the type is int64, which casper.Encode() does not support (by itself). So it will return an error. // ... err := casper.Encode(writer, source) if nil != err { switch err.(type) { case casper.UnsupportedSource: //@TODO default: //@TODO } }
Also, for example:
var source int64 // <---- Note that the type is int64, which casper.Encode() does not support (by itself). So it will return an error. // ... var value casper.Value // ... err := value.Scan(source) if nil != err { switch err.(type) { case casper.UnsupportedSource: //@TODO default: //@TODO } }
type Value ¶
type Value struct {
// contains filtered or unexported fields
}
Value represents a CAPSER value, in the key-value sense of the word “value”.
It is NOT required for this casper.Value type be used to use the tools in this package.
This package works fine without this casper.Value.
casper.Value is provided as a convenience for those looking for a string ‘option type’, that also works seemlessly with the built-in "database/sql", "encoding", "fmt", and "encoding/json" packages.
func NoValue ¶
func NoValue() Value
NoValue creates a casper.Value with no value.
You can use this in if-statements, and switch-statements too.
Example ¶
Here is an example of using casper.NoValue() with an assignment:
var value casper.Value = casper.NoValue()
Example ¶
Here is an example of using casper.NoValue() with an if-statement:
var value casper.Value // ... if casper.NoValue() == value { //@TODO }
Example ¶
Here is an example of using casper.NoValue() with an switch-statement:
var value casper.Value // ... switch value { case casper.NoValue(): //@TODO default: //@TODO }
func SomeValue ¶
SomeValue creates a casper.Value with a specific value.
You can use this in if-statements, and switch-statements too.
Example ¶
Here is an example of using casper.SomeValue() with an assignment:
var value casper.Value = casper.SomeValue("Hello world!")
Example ¶
Here is an example of using casper.SomeValue() with an if-statement:
var value casper.Value // ... if casper.SomeValue("Hello world!") == value { //@TODO }
Example ¶
Here is an example of using casper.SomeValue() with an switch-statement:
var value casper.Value // ... switch value { case casper.SomeValue("Hello world!"): //@TODO default: //@TODO }
func (Value) CASPERValue ¶
func (Value) ElseUnwrap ¶
func (Value) GoString ¶
GoString makes casper.Value fit the fmt.GoStringer interface.
It gets used with the %#v verb with the printing family of functions in the Go built-in "fmt" package.
I.e., it gets used with: fmt.Fprint(), fmt.Fprintf(), fmt.Fprintln(), fmt.Print(), fmt.Printf(), fmt.Println(), fmt.Sprint(), fmt.Sprintf(), fmt.Sprintln().
Example
var value casper.Value // ... fmt.Printf("value = %#v\n", value) // <---- value.GoString() is called by fmt.Printf()
func (*Value) UnmarshalText ¶
Scan makes casper.Value fit the encoding.TextUnmarshaler interface.
Source Files ¶
- encode.go
- encode_delimiter.go
- encode_key.go
- encode_keyvalue.go
- encode_leftcurlybracket.go
- encode_map.go
- encode_newline.go
- encode_rightcurlybracket.go
- encode_semicolon.go
- encode_space.go
- encode_stringvalue.go
- encode_tab.go
- encode_value.go
- errors.go
- shortwrite.go
- stringcaster.go
- unsupportedsource.go
- value.go
- value_caspervalue.go
- value_else.go
- value_elseunwrap.go
- value_gostring.go
- value_map.go
- value_scan.go
- value_string.go
- value_then.go
- value_unmarshaltext.go
- value_unwrap.go
- value_value.go
- valuer.go