Documentation
¶
Overview ¶
Package wire provides shared format and error primitives for structured wire-format packages.
Format-specific behavior lives in the jsonwire, xmlwire, soap, yamlwire, tomlwire, msgpackwire, cborwire, and bsonwire packages. Detection is deliberately limited to opt-in JSON/XML inspection because callers at interoperability boundaries usually know which wire format a peer promises to send.
Example (BsonRoundTrip) ¶
package main
import (
"bytes"
"fmt"
"log"
"github.com/faustbrian/go-wire/bsonwire"
)
func main() {
value := bsonwire.D{{Key: "status", Value: "ok"}}
var output bytes.Buffer
if err := bsonwire.EncodeWriter(&output, value, bsonwire.EncodeOptions{}); err != nil {
log.Fatal(err)
}
var decoded bsonwire.M
if err := bsonwire.Decode(output.Bytes(), &decoded, bsonwire.DecodeOptions{}); err != nil {
log.Fatal(err)
}
fmt.Println(decoded["status"])
}
Output: ok
Example (CborRoundTrip) ¶
package main
import (
"bytes"
"fmt"
"log"
"github.com/faustbrian/go-wire/cborwire"
)
func main() {
value := struct {
Status string `cbor:"status"`
}{Status: "ok"}
var output bytes.Buffer
if err := cborwire.EncodeWriter(&output, value, cborwire.EncodeOptions{}); err != nil {
log.Fatal(err)
}
var decoded struct {
Status string `cbor:"status"`
}
if err := cborwire.Decode(output.Bytes(), &decoded, cborwire.DecodeOptions{}); err != nil {
log.Fatal(err)
}
fmt.Println(decoded.Status)
}
Output: ok
Example (Json) ¶
package main
import (
"fmt"
"log"
"strings"
"github.com/faustbrian/go-wire/jsonwire"
)
func main() {
var response struct {
Status string `json:"status"`
}
err := jsonwire.DecodeReader(
strings.NewReader(`{"status":"ok"}`),
&response,
jsonwire.DecodeOptions{DisallowUnknownFields: true},
)
if err != nil {
log.Fatal(err)
}
fmt.Println(response.Status)
}
Output: ok
Example (JsonWriter) ¶
package main
import (
"bytes"
"fmt"
"log"
"github.com/faustbrian/go-wire/jsonwire"
)
func main() {
var output bytes.Buffer
err := jsonwire.EncodeWriter(&output, map[string]int{"z": 2, "a": 1}, jsonwire.EncodeOptions{})
if err != nil {
log.Fatal(err)
}
fmt.Println(output.String())
}
Output: {"a":1,"z":2}
Example (MessagePackRoundTrip) ¶
package main
import (
"bytes"
"fmt"
"log"
"github.com/faustbrian/go-wire/msgpackwire"
)
func main() {
value := struct {
Status string `msgpack:"status"`
}{Status: "ok"}
var output bytes.Buffer
if err := msgpackwire.EncodeWriter(&output, value, msgpackwire.EncodeOptions{}); err != nil {
log.Fatal(err)
}
var decoded struct {
Status string `msgpack:"status"`
}
if err := msgpackwire.Decode(output.Bytes(), &decoded, msgpackwire.DecodeOptions{}); err != nil {
log.Fatal(err)
}
fmt.Println(decoded.Status)
}
Output: ok
Example (SoapFault) ¶
package main
import (
"errors"
"fmt"
"log"
"github.com/faustbrian/go-wire"
"github.com/faustbrian/go-wire/soap"
)
func main() {
payload, err := soap.MarshalFault(soap.Fault{
Version: soap.Version11,
Code: "soap:Server",
Reason: "Carrier unavailable",
})
if err != nil {
log.Fatal(err)
}
_, err = soap.Parse(payload, soap.ParseOptions{})
if errors.Is(err, wire.ErrSOAPFault) {
fmt.Println("fault")
}
}
Output: fault
Example (SoapWriter) ¶
package main
import (
"bytes"
"encoding/xml"
"fmt"
"log"
"github.com/faustbrian/go-wire/soap"
)
func main() {
var output bytes.Buffer
request := struct {
XMLName xml.Name `xml:"Ping"`
}{}
if err := soap.EncodeWriter(&output, soap.Version11, nil, request, soap.EncodeOptions{}); err != nil {
log.Fatal(err)
}
fmt.Println(output.String())
}
Output: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><Ping></Ping></soap:Body></soap:Envelope>
Example (TomlRoundTrip) ¶
package main
import (
"bytes"
"fmt"
"log"
"github.com/faustbrian/go-wire/tomlwire"
)
func main() {
value := struct {
Status string `toml:"status"`
}{Status: "ok"}
var output bytes.Buffer
if err := tomlwire.EncodeWriter(&output, value, tomlwire.EncodeOptions{}); err != nil {
log.Fatal(err)
}
var decoded struct {
Status string `toml:"status"`
}
if err := tomlwire.Decode(output.Bytes(), &decoded, tomlwire.DecodeOptions{}); err != nil {
log.Fatal(err)
}
fmt.Println(decoded.Status)
}
Output: ok
Example (Xml) ¶
package main
import (
"encoding/xml"
"fmt"
"log"
"github.com/faustbrian/go-wire/xmlwire"
)
func main() {
var shipment struct {
XMLName xml.Name `xml:"urn:vendor Shipment"`
ID int `xml:"urn:vendor ID"`
}
payload := []byte(`<v:Shipment xmlns:v="urn:vendor"><v:ID>42</v:ID></v:Shipment>`)
err := xmlwire.Decode(payload, &shipment, xmlwire.DecodeOptions{
ExpectedRoot: xml.Name{Space: "urn:vendor", Local: "Shipment"},
})
if err != nil {
log.Fatal(err)
}
fmt.Println(shipment.ID)
}
Output: 42
Example (XmlWriter) ¶
package main
import (
"bytes"
"encoding/xml"
"fmt"
"log"
"github.com/faustbrian/go-wire/xmlwire"
)
func main() {
var output bytes.Buffer
value := struct {
XMLName xml.Name `xml:"Status"`
Value string `xml:",chardata"`
}{Value: "ok"}
if err := xmlwire.EncodeWriter(&output, value, xmlwire.EncodeOptions{}); err != nil {
log.Fatal(err)
}
fmt.Println(output.String())
}
Output: <Status>ok</Status>
Example (YamlRoundTrip) ¶
package main
import (
"bytes"
"fmt"
"log"
"github.com/faustbrian/go-wire/yamlwire"
)
func main() {
value := struct {
Status string `yaml:"status"`
}{Status: "ok"}
var output bytes.Buffer
if err := yamlwire.EncodeWriter(&output, value, yamlwire.EncodeOptions{}); err != nil {
log.Fatal(err)
}
var decoded struct {
Status string `yaml:"status"`
}
if err := yamlwire.Decode(output.Bytes(), &decoded, yamlwire.DecodeOptions{}); err != nil {
log.Fatal(err)
}
fmt.Println(decoded.Status)
}
Output: ok
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrParse = errors.New("parse failure") ErrValidation = errors.New("validation failure") ErrUnsupportedFormat = errors.New("unsupported format") ErrEnvelope = errors.New("envelope failure") ErrSOAPFault = errors.New("SOAP fault") ErrWrite = errors.New("write failure") ErrSizeLimit = errors.New("size limit exceeded") ErrTarget = errors.New("invalid target") ErrEncode = errors.New("encode failure") )
Functions ¶
This section is empty.
Types ¶
type Error ¶
Error describes a wire-format operation failure.
type ErrorKind ¶
type ErrorKind string
ErrorKind classifies a failure independently of its underlying cause.
const ( ErrorKindParse ErrorKind = "parse" ErrorKindValidation ErrorKind = "validation" ErrorKindUnsupported ErrorKind = "unsupported" ErrorKindEnvelope ErrorKind = "envelope" ErrorKindFault ErrorKind = "fault" ErrorKindWrite ErrorKind = "write" ErrorKindSizeLimit ErrorKind = "size-limit" ErrorKindTarget ErrorKind = "target" ErrorKindEncode ErrorKind = "encode" )
type Format ¶
type Format string
Format identifies a supported structured wire format.
func DetectFormat ¶
DetectFormat distinguishes JSON from XML using the first significant byte. SOAP is returned as XML because reliably identifying a SOAP envelope requires parsing its namespace; use the soap package when SOAP is expected.
Example ¶
package main
import (
"fmt"
"log"
"github.com/faustbrian/go-wire"
)
func main() {
format, err := wire.DetectFormat([]byte(`{"status":"ok"}`))
if err != nil {
log.Fatal(err)
}
fmt.Println(format)
}
Output: json
Directories
¶
| Path | Synopsis |
|---|---|
|
Package bsonwire provides bounded BSON document decoding and encoding.
|
Package bsonwire provides bounded BSON document decoding and encoding. |
|
Package cborwire provides bounded CBOR decoding and deterministic encoding.
|
Package cborwire provides bounded CBOR decoding and deterministic encoding. |
|
cmd
|
|
|
semvercheck
command
|
|
|
internal
|
|
|
outputlimit
Package outputlimit provides bounded in-memory encoding destinations.
|
Package outputlimit provides bounded in-memory encoding destinations. |
|
valuecheck
Package valuecheck validates reflection values before recursive encoding.
|
Package valuecheck validates reflection values before recursive encoding. |
|
Package jsonwire provides bounded, strict JSON helpers for interoperability boundaries where encoding/json's low-level defaults need explicit policy.
|
Package jsonwire provides bounded, strict JSON helpers for interoperability boundaries where encoding/json's low-level defaults need explicit policy. |
|
Package msgpackwire provides bounded MessagePack decoding and deterministic encoding.
|
Package msgpackwire provides bounded MessagePack decoding and deterministic encoding. |
|
Package soap provides transport-neutral SOAP 1.1 and SOAP 1.2 envelope, body, and fault primitives while preserving access to the original XML.
|
Package soap provides transport-neutral SOAP 1.1 and SOAP 1.2 envelope, body, and fault primitives while preserving access to the original XML. |
|
Package tomlwire provides bounded, strict TOML document decoding and deterministic encoding.
|
Package tomlwire provides bounded, strict TOML document decoding and deterministic encoding. |
|
Package xmlwire provides bounded XML encoding and decoding with explicit namespace, root-element, strictness, and character-set policy.
|
Package xmlwire provides bounded XML encoding and decoding with explicit namespace, root-element, strictness, and character-set policy. |
|
Package yamlwire provides bounded, explicit YAML 1.2 decoding and deterministic encoding.
|
Package yamlwire provides bounded, explicit YAML 1.2 decoding and deterministic encoding. |