Repository: qpid-proton Updated Branches: refs/heads/0.9.x [created] adef38209
removed incomplete go binding Project: http://git-wip-us.apache.org/repos/asf/qpid-proton/repo Commit: http://git-wip-us.apache.org/repos/asf/qpid-proton/commit/e0671e7b Tree: http://git-wip-us.apache.org/repos/asf/qpid-proton/tree/e0671e7b Diff: http://git-wip-us.apache.org/repos/asf/qpid-proton/diff/e0671e7b Branch: refs/heads/0.9.x Commit: e0671e7bd61975d42fcd0bd24f47a8067daae637 Parents: 0b4e9ba Author: Rafael Schloming <[email protected]> Authored: Tue Mar 10 00:37:22 2015 +1300 Committer: Rafael Schloming <[email protected]> Committed: Tue Mar 10 00:37:22 2015 +1300 ---------------------------------------------------------------------- proton-c/bindings/go/README.md | 133 ------------ proton-c/bindings/go/WARNING_EXPERIMENTAL | 1 - .../bindings/go/src/apache.org/proton/doc.go | 33 --- .../go/src/apache.org/proton/encoding.go | 216 ------------------- .../bindings/go/src/apache.org/proton/error.go | 91 -------- .../go/src/apache.org/proton/interop_test.go | 121 ----------- .../bindings/go/src/apache.org/proton/types.go | 25 --- .../bindings/go/src/apache.org/proton/url.go | 96 --------- .../go/src/apache.org/proton/url_test.go | 51 ----- 9 files changed, 767 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/e0671e7b/proton-c/bindings/go/README.md ---------------------------------------------------------------------- diff --git a/proton-c/bindings/go/README.md b/proton-c/bindings/go/README.md deleted file mode 100644 index 31bdb5d..0000000 --- a/proton-c/bindings/go/README.md +++ /dev/null @@ -1,133 +0,0 @@ -# *EXPERIMENTAL* Go binding for proton - -This is the beginning of a [Go](http://golang.org) binding for proton. - -This work is in very early *experimental* stages, *everything* might change in -future. Comments and contributions are strongly encouraged, this experiment is -public so early feedback can guide development. - -- Email <[email protected]> -- Create issues <https://issues.apache.org/jira/browse/PROTON>, attach patches to an issue. - -## Goals - -The API will be inspired by the reactive, event-driven python API. Key features: - -- support client and server development. -- incremental composition of functionality via handlers. -- default handlers to make simple tasks simple. -- deep access to AMQP protocol events when that is required. - -The API will be idiomatic, unsurprising, and easy to use for Go developers. - -There are two types of developer we want to support - -1. For Go developers using AMQP as a message transport: - - Straightforward conversions between Go built-in types and AMQP types. - - Easy message exchange via Go channels to support use in goroutines. - -2. For AMQP-aware developers using Go as an implementation language: - - Go types to exactly represent all AMQP types and encoding details. - - Full access to AMQP concepts like connections, sessions and links via handler interfaces. - -We will follow conventions of the C and python API where possible to help -cross-language developers but idiomatic Go is the overriding consideration. - -## Status - -So just a simple Go `Url` type using `pn_url_t`. This establishes the basics of -using cgo to call into proton code. - -## Layout - -This directory is a [Go work-space](http://golang.org/doc/code.html), it is not -yet connected to the rest of the proton build. - -To experiment, install proton in a standard place or set these environment -variables: `PATH`, `C_INCLUDE_PATH`, `LIBRARY_PATH` and `LD_LIBRARY_PATH`. - -Add this directory to `GOPATH` for the Go tools. - -To see the docs as text: - - godoc apache.org/proton - -To see them in your browser run this in the background and open -http://localhost:6060 in your browser: - - godoc -http=:6060 -index=true& - -Click "Packages" and "proton" to see the proton docs. It takes a minute or two -to generate the index so search may not work immediately. - -To run the unit tests: - - go test -a apache.org/proton - -## New to Go? - -If you are new to Go then these are a good place to start: - -- [A Tour of Go](http://tour.golang.org) -- [Effective Go](http://golang.org/doc/effective_go.html) - -Then look at the tools and library docs at <http://golang.org> as you need them. - -## Design Notes - -### C wrapping philosophy - -We use `cgo` to call proton C functions directly. `cgo` is simpler and more -direct than Swig and integrated into the Go tools. - -Calling C directly from Go is so easy that we will avoid low-level 1-1 wrappers -for proton objects and focus on easy to use Go types that live within Go's -automatic memory management. - -Programmers that need lower-level access than we provide can go direct to C, but -of course we will aim to make that unnecessary in all but the most unusual cases. - -### Other considerations - -Go's use of channels for synchronization present interesting opportunities. Go -also supports traditional locking, so we could adopt locking strategies similar -to our other bindings, but we should investigate the Go-like alternatives. There -are analogies between Go channels and AMQP links that we will probably exploit. - -## Implementation status - -Working on API to marshal/unmarshal AMQP data into Go types. - -The API will follow the style of the standard libraries encoding/json and encoding/xml. - -To be done: - -Easy unmarshaling into native Go types: - -- String-like AMQP types (symbol, binary, string) into Go string or []byte -- Numeric AMQP types into any Go numeric (numeric conversion) -- Any AMQP type into GO reflect.Value choosing the closest native Go type -- AMQP map into go struct if keys match struct field names and values match field types -- AMQP maps into map[K]T if all AMQP keys/values can convert to K and T (reflect.Value allowed) -- AMQP list into []T if all list elements can convert to T (reflect.Value allowed) - -Easy marshaling of native Go types: - -- Go struct: amqp map with field names as string keys (use tags to customize?) -- Go string to AMQP string, Go []byte to AMQP binary, Go numerics to closest AMQP numeric -- Go []T to AMQP list -- Go map to AMQP map - -Customization: - -- Standard encoding libraries define Marshaler and Unmarshaler interfaces. -- User implements to customize behavior of a user type. -- Does this require exposing the proton codec? - -Exact (strict) (un)marshaling: - -- Define special Go AMQP types that exactly reflect AMQP types & encodings. -- Unmarshal to special types only if exact match for wire -- Marshal special types exactly -- Define AMQPValue which can unmarshal from any AMQP type using strict unmarshaling types. - http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/e0671e7b/proton-c/bindings/go/WARNING_EXPERIMENTAL ---------------------------------------------------------------------- diff --git a/proton-c/bindings/go/WARNING_EXPERIMENTAL b/proton-c/bindings/go/WARNING_EXPERIMENTAL deleted file mode 100644 index 96dc92f..0000000 --- a/proton-c/bindings/go/WARNING_EXPERIMENTAL +++ /dev/null @@ -1 +0,0 @@ -See README.md http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/e0671e7b/proton-c/bindings/go/src/apache.org/proton/doc.go ---------------------------------------------------------------------- diff --git a/proton-c/bindings/go/src/apache.org/proton/doc.go b/proton-c/bindings/go/src/apache.org/proton/doc.go deleted file mode 100644 index 34f85fe..0000000 --- a/proton-c/bindings/go/src/apache.org/proton/doc.go +++ /dev/null @@ -1,33 +0,0 @@ -/* -Licensed to the Apache Software Foundation (ASF) under one -or more contributor license agreements. See the NOTICE file -distributed with this work for additional information -regarding copyright ownership. The ASF licenses this file -to you under the Apache License, Version 2.0 (the -"License"); you may not use this file except in compliance -with the License. You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, -software distributed under the License is distributed on an -"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -KIND, either express or implied. See the License for the -specific language governing permissions and limitations -under the License. -*/ - -/* -Package proton is a Go binding for the proton AMQP protocol engine. - -It alows you to construct and parse AMQP messages, and to implement AMQP -clients, servers and intermediaries that can exchange messages with any -AMQP 1.0 compliant endpoint. - -Encoding and decoding AMQP data follows the pattern of the standard -encoding/json and encoding/xml packages.The mapping between AMQP and Go types is -described in the documentation of the Marshal and Unmarshal functions. -*/ -package proton - -// This file is just for the package comment. http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/e0671e7b/proton-c/bindings/go/src/apache.org/proton/encoding.go ---------------------------------------------------------------------- diff --git a/proton-c/bindings/go/src/apache.org/proton/encoding.go b/proton-c/bindings/go/src/apache.org/proton/encoding.go deleted file mode 100644 index eb42c8c..0000000 --- a/proton-c/bindings/go/src/apache.org/proton/encoding.go +++ /dev/null @@ -1,216 +0,0 @@ -/* -Licensed to the Apache Software Foundation (ASF) under one -or more contributor license agreements. See the NOTICE file -distributed with this work for additional information -regarding copyright ownership. The ASF licenses this file -to you under the Apache License, Version 2.0 (the -"License"); you may not use this file except in compliance -with the License. You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, -software distributed under the License is distributed on an -"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -KIND, either express or implied. See the License for the -specific language governing permissions and limitations -under the License. -*/ - -package proton - -//#include <proton/codec.h> -import "C" - -import ( - "bytes" - "io" - "reflect" - "unsafe" -) - -// -// Decoding from a pn_data_t -// - -type pnDecoder struct{ data *C.pn_data_t } - -func newPnDecoder() pnDecoder { return pnDecoder{C.pn_data(0)} } -func (pd pnDecoder) free() { C.pn_data_free((*C.pn_data_t)(pd.data)) } - -// decode from bytes. Return bytes decoded and errEOS if we run out of data. -func (pd pnDecoder) decode(bytes []byte) (n int, err error) { - C.pn_data_clear(pd.data) - if len(bytes) == 0 { - return 0, errEOS - } - cBuf := (*C.char)(unsafe.Pointer(&bytes[0])) - result := int(C.pn_data_decode(pd.data, cBuf, C.size_t(len(bytes)))) - if result < 0 { - return 0, errorCode(result) - } else { - return result, nil - } -} - -// Unmarshal decodes bytes and converts into the value pointed to by v. -// -// Returns the number of bytes decoded and an errorCode on error. -func (pd pnDecoder) unmarshal(bytes []byte, v interface{}) (n int, err error) { - n, err = pd.decode(bytes) - if err != nil { - return - } - switch v := v.(type) { - case *string: - err = pd.unmarshalString(v) - case *[]byte: - err = pd.unmarshalBytes(v) - case *Symbol: - err = pd.unmarshalSymbol(v) - default: - note := "" - if reflect.TypeOf(v).Kind() != reflect.Ptr { - note = "is not a pointer" - } - return 0, errorf("Unmarshal bad type: %T %s", v, note) - // FIXME aconway 2015-03-02: not finished - } - if err != nil { - return 0, err - } - return -} - -func (pd pnDecoder) unmarshalPnBytes(target string) (pnBytes C.pn_bytes_t, err error) { - switch amqpType := C.pn_data_type(pd.data); amqpType { - case C.PN_STRING: - pnBytes = C.pn_data_get_string(pd.data) - case C.PN_BINARY: - pnBytes = C.pn_data_get_binary(pd.data) - case C.PN_SYMBOL: - pnBytes = C.pn_data_get_symbol(pd.data) - default: - // FIXME aconway 2015-03-02: error message - json style UnmarsalTypeError? - return C.pn_bytes_t{}, errorf("Unmarshal cannot convert %#v to %s", amqpType, target) - } - return pnBytes, nil -} - -func (pd pnDecoder) unmarshalString(v *string) error { - pnBytes, err := pd.unmarshalPnBytes("string") - if err == nil { - *v = C.GoStringN(pnBytes.start, C.int(pnBytes.size)) - } - return err -} - -func (pd pnDecoder) unmarshalBytes(v *[]byte) error { - pnBytes, err := pd.unmarshalPnBytes("[]byte") - *v = C.GoBytes(unsafe.Pointer(pnBytes.start), C.int(pnBytes.size)) - return err -} - -func (pd pnDecoder) unmarshalSymbol(v *Symbol) error { - pnBytes, err := pd.unmarshalPnBytes("symbol") - if err == nil { - *v = Symbol(C.GoStringN(pnBytes.start, C.int(pnBytes.size))) - } - return err -} - -/* -Unmarshal decodes AMQP-encoded bytes and stores the result in the value pointed to by v. - -FIXME mapping details - - +-------------------------------+-----------------------------------------------+ - |AMQP type |Go type | - +-------------------------------+-----------------------------------------------+ - |string |string | - +-------------------------------+-----------------------------------------------+ - |symbol |proton.Symbol | - +-------------------------------+-----------------------------------------------+ - |binary |[]byte | - +-------------------------------+-----------------------------------------------+ -*/ -func Unmarshal(bytes []byte, v interface{}) error { - pd := newPnDecoder() - defer pd.free() - _, err := pd.unmarshal(bytes, v) - return err -} - -// Decoder decodes AMQP values from an io.Reader. -// -type Decoder struct { - reader io.Reader - buffer bytes.Buffer - readErr error // Outstanding error on our reader -} - -// NewDecoder returns a new decoder that reads from r. -// -// The decoder has it's own buffer and may read more data than required for the -// AMQP values requested. Use Buffered to see if there is data left in the -// buffer. -// -func NewDecoder(r io.Reader) *Decoder { - return &Decoder{r, bytes.Buffer{}, nil} -} - -// Buffered returns a reader of the data remaining in the Decoder's buffer. The -// reader is valid until the next call to Decode. -// -func (d *Decoder) Buffered() io.Reader { - return bytes.NewReader(d.buffer.Bytes()) -} - -// more reads more data when we can't parse a complete AMQP type -func (d *Decoder) more() error { - if d.readErr != nil { // Reader already broken, give up - return d.readErr - } - var readSize int64 = 256 - if int64(d.buffer.Len()) > readSize { // Grow by doubling - readSize = int64(d.buffer.Len()) - } - var n int64 - n, d.readErr = d.buffer.ReadFrom(&io.LimitedReader{d.reader, readSize}) - if n == 0 { // ReadFrom won't report io.EOF, just returns 0 - if d.readErr != nil { - return d.readErr - } else { - return errorf("no data") - } - } - return nil -} - -// Decode reads the next AMQP value from the Reader and stores it in the value pointed to by v. -// -// See the documentation for Unmarshal for details about the conversion of AMQP into a Go value. -// -func (d *Decoder) Decode(v interface{}) (err error) { - pd := newPnDecoder() - defer pd.free() - - // On errEOS, read more data and try again till we have a complete pn_data. - for { - var n int - n, err = pd.unmarshal(d.buffer.Bytes(), v) - switch err { - case nil: - d.buffer.Next(n) - return - case errEOS: - err = d.more() - if err != nil { - return err - } - default: - return err - } - } - return err -} http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/e0671e7b/proton-c/bindings/go/src/apache.org/proton/error.go ---------------------------------------------------------------------- diff --git a/proton-c/bindings/go/src/apache.org/proton/error.go b/proton-c/bindings/go/src/apache.org/proton/error.go deleted file mode 100644 index 51cde28..0000000 --- a/proton-c/bindings/go/src/apache.org/proton/error.go +++ /dev/null @@ -1,91 +0,0 @@ -/* -Licensed to the Apache Software Foundation (ASF) under one -or more contributor license agreements. See the NOTICE file -distributed with this work for additional information -regarding copyright ownership. The ASF licenses this file -to you under the Apache License, Version 2.0 (the -"License"); you may not use this file except in compliance -with the License. You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, -software distributed under the License is distributed on an -"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -KIND, either express or implied. See the License for the -specific language governing permissions and limitations -under the License. -*/ - -package proton - -// #include <proton/error.h> -import "C" - -import ( - "fmt" -) - -// errorCode is an error code returned by proton C. -type errorCode int - -const ( - errEOS errorCode = C.PN_EOS - errError = C.PN_ERR - errOverflow = C.PN_OVERFLOW - errUnderflow = C.PN_UNDERFLOW - errState = C.PN_STATE_ERR - errArgument = C.PN_ARG_ERR - errTimeout = C.PN_TIMEOUT - errInterrupted = C.PN_INTR - errInProgress = C.PN_INPROGRESS -) - -// String gives a brief description of an errorCode. -func (code errorCode) String() string { - switch code { - case errEOS: - return "end of data" - case errError: - return "error" - case errOverflow: - return "overflow" - case errUnderflow: - return "underflow" - case errState: - return "bad state" - case errArgument: - return "invalid argument" - case errTimeout: - return "timeout" - case errInterrupted: - return "interrupted" - case errInProgress: - return "in progress" - } - return fmt.Sprintf("invalid error code %d", code) -} - -// An errorCode can be used as an error -func (code errorCode) Error() string { - return fmt.Sprintf("proton: %v", code) -} - -// pnError is a simple error string. -// -// NOTE: All error types used in proton have both String() and Error() methods. -// The String() method prints the plain error message, the Error() method -// prints the error message with a "proton:" prefix. -// Thus you can format nested error messages with "%s" without getting nested "proton:" -// prefixes but the prefix will be added when the end user uses Error() -// or "%v" on the error value. -// -type pnError string - -func (err pnError) String() string { return string(err) } -func (err pnError) Error() string { return fmt.Sprintf("proton: %s", string(err)) } - -// errorf creates an error with a formatted message -func errorf(format string, a ...interface{}) error { - return pnError(fmt.Sprintf(format, a...)) -} http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/e0671e7b/proton-c/bindings/go/src/apache.org/proton/interop_test.go ---------------------------------------------------------------------- diff --git a/proton-c/bindings/go/src/apache.org/proton/interop_test.go b/proton-c/bindings/go/src/apache.org/proton/interop_test.go deleted file mode 100644 index db988d2..0000000 --- a/proton-c/bindings/go/src/apache.org/proton/interop_test.go +++ /dev/null @@ -1,121 +0,0 @@ -/* -Licensed to the Apache Software Foundation (ASF) under one -or more contributor license agreements. See the NOTICE file -distributed with this work for additional information -regarding copyright ownership. The ASF licenses this file -to you under the Apache License, Version 2.0 (the -"License"); you may not use this file except in compliance -with the License. You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, -software distributed under the License is distributed on an -"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -KIND, either express or implied. See the License for the -specific language governing permissions and limitations -under the License. -*/ - -// Test that conversion of Go type to/from AMQP is compatible with other -// bindings. -// -// FIXME aconway 2015-03-01: this should move to proton/tests/go when we integrate -// better with the proton build system. -// -package proton - -import ( - "fmt" - "io" - "io/ioutil" - "os" - "reflect" - "testing" -) - -func getReader(t *testing.T, name string) (r io.Reader) { - r, err := os.Open("../../../../../../tests/interop/" + name + ".amqp") - if err != nil { - t.Fatalf("Can't open %#v: %v", name, err) - } - return -} - -func remaining(d *Decoder) string { - remainder, _ := ioutil.ReadAll(io.MultiReader(d.Buffered(), d.reader)) - return string(remainder) -} - -// Expectation of a test, want is the expected value, got is a pointer to a -// instance of the same type, which will be replaced by Decode. -type expect struct { - want, got interface{} -} - -// checkDecode: want is the expected value, gotPtr is a pointer to a -// instance of the same type for Decode. -func checkDecode(d *Decoder, want interface{}, gotPtr interface{}) error { - err := d.Decode(gotPtr) - if err != nil { - return err - } - got := reflect.ValueOf(gotPtr).Elem().Interface() - if !reflect.DeepEqual(want, got) { - return fmt.Errorf("%#v != %#v", want, got) - } - return nil -} - -func TestUnmarshal(t *testing.T) { - bytes, err := ioutil.ReadAll(getReader(t, "strings")) - if err != nil { - t.Error(err) - } - var got string - err = Unmarshal(bytes, &got) - if err != nil { - t.Error(err) - } - want := "abc\000defg" - if want != got { - t.Errorf("%#v != %#v", want, got) - } -} - -func TestStrings(t *testing.T) { - d := NewDecoder(getReader(t, "strings")) - // Test decoding as plain Go strings - for i, want := range []string{"abc\000defg", "abcdefg", "abcdefg", "", "", ""} { - var got string - if err := checkDecode(d, want, &got); err != nil { - t.Errorf("%d: %v", i, err) - } - } - remains := remaining(d) - if remains != "" { - t.Errorf("leftover: %s", remains) - } - - // Test decoding as specific string types - d = NewDecoder(getReader(t, "strings")) - var bytes []byte - var str string - var sym Symbol - for i, expect := range []expect{ - {[]byte("abc\000defg"), &bytes}, - {"abcdefg", &str}, - {Symbol("abcdefg"), &sym}, - {make([]byte, 0), &bytes}, - {"", &str}, - {Symbol(""), &sym}, - } { - if err := checkDecode(d, expect.want, expect.got); err != nil { - t.Errorf("%d: %v", i, err) - } - } - remains = remaining(d) - if remains != "" { - t.Errorf("leftover: %s", remains) - } -} http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/e0671e7b/proton-c/bindings/go/src/apache.org/proton/types.go ---------------------------------------------------------------------- diff --git a/proton-c/bindings/go/src/apache.org/proton/types.go b/proton-c/bindings/go/src/apache.org/proton/types.go deleted file mode 100644 index bde1ddd..0000000 --- a/proton-c/bindings/go/src/apache.org/proton/types.go +++ /dev/null @@ -1,25 +0,0 @@ -/* -Licensed to the Apache Software Foundation (ASF) under one -or more contributor license agreements. See the NOTICE file -distributed with this work for additional information -regarding copyright ownership. The ASF licenses this file -to you under the Apache License, Version 2.0 (the -"License"); you may not use this file except in compliance -with the License. You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, -software distributed under the License is distributed on an -"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -KIND, either express or implied. See the License for the -specific language governing permissions and limitations -under the License. -*/ - -package proton - -// Types to exactly represent specific AMQP encodings - -// Symbol is the AMQP symbol data type, it can be converted to a Go string or []byte -type Symbol string http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/e0671e7b/proton-c/bindings/go/src/apache.org/proton/url.go ---------------------------------------------------------------------- diff --git a/proton-c/bindings/go/src/apache.org/proton/url.go b/proton-c/bindings/go/src/apache.org/proton/url.go deleted file mode 100644 index 1511203..0000000 --- a/proton-c/bindings/go/src/apache.org/proton/url.go +++ /dev/null @@ -1,96 +0,0 @@ -/* -Licensed to the Apache Software Foundation (ASF) under one -or more contributor license agreements. See the NOTICE file -distributed with this work for additional information -regarding copyright ownership. The ASF licenses this file -to you under the Apache License, Version 2.0 (the -"License"); you may not use this file except in compliance -with the License. You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, -software distributed under the License is distributed on an -"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -KIND, either express or implied. See the License for the -specific language governing permissions and limitations -under the License. -*/ - -package proton - -/* -#cgo LDFLAGS: -lqpid-proton -#include <stdlib.h> -#include <string.h> -#include <proton/url.h> - -// Helper function for setting URL fields. -typedef void (*setter_fn)(pn_url_t* url, const char* value); -inline void set(pn_url_t *url, setter_fn s, const char* value) { - s(url, value); -} -*/ -import "C" - -import ( - "net" - "net/url" - "unsafe" -) - -const ( - AMQP string = "amqp" - AMQPS = "amqps" -) - -// ParseUrl parses an AMQP URL string and returns a net/url.Url. -// -// It is more forgiving than net/url.Parse and allows most of the parts of the -// URL to be missing, assuming AMQP defaults. -// -func ParseURL(s string) (u *url.URL, err error) { - cstr := C.CString(s) - defer C.free(unsafe.Pointer(cstr)) - pnUrl := C.pn_url_parse(cstr) - if pnUrl == nil { - return nil, errorf("bad URL %#v", s) - } - defer C.pn_url_free(pnUrl) - - scheme := C.GoString(C.pn_url_get_scheme(pnUrl)) - username := C.GoString(C.pn_url_get_username(pnUrl)) - password := C.GoString(C.pn_url_get_password(pnUrl)) - host := C.GoString(C.pn_url_get_host(pnUrl)) - port := C.GoString(C.pn_url_get_port(pnUrl)) - path := C.GoString(C.pn_url_get_path(pnUrl)) - - if err != nil { - return nil, errorf("bad URL %#v: %s", s, err) - } - if scheme == "" { - scheme = AMQP - } - if port == "" { - if scheme == AMQPS { - port = AMQPS - } else { - port = AMQP - } - } - var user *url.Userinfo - if password != "" { - user = url.UserPassword(username, password) - } else if username != "" { - user = url.User(username) - } - - u = &url.URL{ - Scheme: scheme, - User: user, - Host: net.JoinHostPort(host, port), - Path: path, - } - - return u, nil -} http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/e0671e7b/proton-c/bindings/go/src/apache.org/proton/url_test.go ---------------------------------------------------------------------- diff --git a/proton-c/bindings/go/src/apache.org/proton/url_test.go b/proton-c/bindings/go/src/apache.org/proton/url_test.go deleted file mode 100644 index 7315511..0000000 --- a/proton-c/bindings/go/src/apache.org/proton/url_test.go +++ /dev/null @@ -1,51 +0,0 @@ -/* -Licensed to the Apache Software Foundation (ASF) under one -or more contributor license agreements. See the NOTICE file -distributed with this work for additional information -regarding copyright ownership. The ASF licenses this file -to you under the Apache License, Version 2.0 (the -"License"); you may not use this file except in compliance -with the License. You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, -software distributed under the License is distributed on an -"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -KIND, either express or implied. See the License for the -specific language governing permissions and limitations -under the License. -*/ - -package proton - -import ( - "fmt" -) - -func ExampleParseURL() { - for _, s := range []string{ - "amqp://username:password@host:1234/path", - "host:1234", - "host", - ":1234", - "host/path", - "amqps://host", - "", - } { - u, err := ParseURL(s) - if err != nil { - fmt.Println(err) - } else { - fmt.Println(u) - } - } - // Output: - // amqp://username:password@host:1234/path - // amqp://host:1234 - // amqp://host:amqp - // amqp://:1234 - // amqp://host:amqp/path - // amqps://host:amqps - // proton: bad URL "" -} --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
