Merge pull request #14 from jwetzell/pointers

return pointers from byte parsing
This commit is contained in:
Joel Wetzell
2026-01-31 15:39:51 -06:00
committed by GitHub
4 changed files with 37 additions and 37 deletions
+13 -13
View File
@@ -21,25 +21,25 @@ func (b *OSCBundle) ToBytes() []byte {
return bytes return bytes
} }
func BundleFromBytes(bytes []byte) (OSCBundle, []byte, error) { func BundleFromBytes(bytes []byte) (*OSCBundle, []byte, error) {
if len(bytes) < 20 { if len(bytes) < 20 {
return OSCBundle{}, bytes, errors.New("bundle has to be at least 20 bytes") return nil, bytes, errors.New("bundle has to be at least 20 bytes")
} }
if bytes[0] != 35 { if bytes[0] != 35 {
return OSCBundle{}, bytes, errors.New("bundle must start with a #") return nil, bytes, errors.New("bundle must start with a #")
} }
bundleHeader, bytesAfterBundleHeader := readOSCString(bytes) bundleHeader, bytesAfterBundleHeader := readOSCString(bytes)
if bundleHeader != "#bundle" { if bundleHeader != "#bundle" {
return OSCBundle{}, bytesAfterBundleHeader, errors.New("bundle must start with #bundle string") return nil, bytesAfterBundleHeader, errors.New("bundle must start with #bundle string")
} }
timeTag, bytesAfterTimeTag, err := readOSCTimeTag(bytesAfterBundleHeader) timeTag, bytesAfterTimeTag, err := readOSCTimeTag(bytesAfterBundleHeader)
if err != nil { if err != nil {
return OSCBundle{}, bytesAfterBundleHeader, err return nil, bytesAfterBundleHeader, err
} }
bundleContents := []OSCPacket{} bundleContents := []OSCPacket{}
@@ -52,13 +52,13 @@ func BundleFromBytes(bytes []byte) (OSCBundle, []byte, error) {
contentSize, bytesAfterContentSize, err := readOSCInt32(remainingBytes) contentSize, bytesAfterContentSize, err := readOSCInt32(remainingBytes)
if err != nil { if err != nil {
return OSCBundle{}, remainingBytes, err return nil, remainingBytes, err
} }
remainingBytes = bytesAfterContentSize remainingBytes = bytesAfterContentSize
if len(remainingBytes) < int(contentSize) { if len(remainingBytes) < int(contentSize) {
return OSCBundle{}, remainingBytes, errors.New("bundle doesn't have enough bytes for the content size it specifies") return nil, remainingBytes, errors.New("bundle doesn't have enough bytes for the content size it specifies")
} }
bundleContentBytes := remainingBytes[0:contentSize] bundleContentBytes := remainingBytes[0:contentSize]
@@ -66,17 +66,17 @@ func BundleFromBytes(bytes []byte) (OSCBundle, []byte, error) {
if bundleContentBytes[0] == 35 { if bundleContentBytes[0] == 35 {
content, _, err := BundleFromBytes(bundleContentBytes) content, _, err := BundleFromBytes(bundleContentBytes)
if err != nil { if err != nil {
return OSCBundle{}, remainingBytes, err return nil, remainingBytes, err
} }
bundleContents = append(bundleContents, &content) bundleContents = append(bundleContents, content)
} else if bundleContentBytes[0] == 47 { } else if bundleContentBytes[0] == 47 {
content, err := MessageFromBytes(bundleContentBytes) content, err := MessageFromBytes(bundleContentBytes)
if err != nil { if err != nil {
return OSCBundle{}, remainingBytes, err return nil, remainingBytes, err
} }
bundleContents = append(bundleContents, &content) bundleContents = append(bundleContents, content)
} else { } else {
return OSCBundle{}, remainingBytes, errors.New("bundle contents does not look a bundle or message") return nil, remainingBytes, errors.New("bundle contents does not look a bundle or message")
} }
remainingBytes = bytesAfterContentSize[contentSize:] remainingBytes = bytesAfterContentSize[contentSize:]
if len(remainingBytes) == 0 { if len(remainingBytes) == 0 {
@@ -85,7 +85,7 @@ func BundleFromBytes(bytes []byte) (OSCBundle, []byte, error) {
} }
return OSCBundle{ return &OSCBundle{
TimeTag: timeTag, TimeTag: timeTag,
Contents: bundleContents, Contents: bundleContents,
}, },
+4 -4
View File
@@ -10,12 +10,12 @@ func TestOSCBundleEncoding(t *testing.T) {
testCases := []struct { testCases := []struct {
description string description string
bundle OSCBundle bundle *OSCBundle
expected []byte expected []byte
}{ }{
{ {
"simple contents single message", "simple contents single message",
OSCBundle{ &OSCBundle{
TimeTag: OSCTimeTag{ TimeTag: OSCTimeTag{
seconds: 32, seconds: 32,
fractionalSeconds: 0, fractionalSeconds: 0,
@@ -46,12 +46,12 @@ func TestOSCBundleEncoding(t *testing.T) {
func TestOSCBundleDecoding(t *testing.T) { func TestOSCBundleDecoding(t *testing.T) {
testCases := []struct { testCases := []struct {
description string description string
expected OSCBundle expected *OSCBundle
bytes []byte bytes []byte
}{ }{
{ {
"simple contents single message", "simple contents single message",
OSCBundle{ &OSCBundle{
TimeTag: OSCTimeTag{ TimeTag: OSCTimeTag{
seconds: 32, seconds: 32,
fractionalSeconds: 0, fractionalSeconds: 0,
+6 -6
View File
@@ -25,15 +25,15 @@ func (m *OSCMessage) ToBytes() []byte {
return oscBuffer return oscBuffer
} }
func MessageFromBytes(bytes []byte) (OSCMessage, error) { func MessageFromBytes(bytes []byte) (*OSCMessage, error) {
if len(bytes) == 0 { if len(bytes) == 0 {
return OSCMessage{}, errors.New("cannot create OSC Message from empty byte array") return nil, errors.New("cannot create OSC Message from empty byte array")
} }
address, typeAndArgBytes := readOSCString(bytes) address, typeAndArgBytes := readOSCString(bytes)
if address[0] != 47 { if address[0] != 47 {
return OSCMessage{}, errors.New("OSC Message address must start with /") return nil, errors.New("OSC Message address must start with /")
} }
oscMessage := OSCMessage{ oscMessage := OSCMessage{
@@ -46,17 +46,17 @@ func MessageFromBytes(bytes []byte) (OSCMessage, error) {
for index, oscType := range typeString { for index, oscType := range typeString {
if index == 0 { if index == 0 {
if oscType != ',' { if oscType != ',' {
return OSCMessage{}, errors.New("type string is malformed") return nil, errors.New("type string is malformed")
} }
} else { } else {
oscArg, remainingBytes, error := readOSCArg(argBytes, string(oscType)) oscArg, remainingBytes, error := readOSCArg(argBytes, string(oscType))
if error != nil { if error != nil {
return oscMessage, error return nil, error
} }
argBytes = remainingBytes argBytes = remainingBytes
oscMessage.Args = append(oscMessage.Args, oscArg) oscMessage.Args = append(oscMessage.Args, oscArg)
} }
} }
return oscMessage, nil return &oscMessage, nil
} }
+14 -14
View File
@@ -11,12 +11,12 @@ func TestOSCMessageEncoding(t *testing.T) {
testCases := []struct { testCases := []struct {
description string description string
message OSCMessage message *OSCMessage
expected []byte expected []byte
}{ }{
{ {
"simple hello", "simple hello",
OSCMessage{ &OSCMessage{
Address: "/hello", Address: "/hello",
Args: []OSCArg{}, Args: []OSCArg{},
}, },
@@ -24,7 +24,7 @@ func TestOSCMessageEncoding(t *testing.T) {
}, },
{ {
"simple address string arg", "simple address string arg",
OSCMessage{ &OSCMessage{
Address: "/hello", Address: "/hello",
Args: []OSCArg{ Args: []OSCArg{
{ {
@@ -37,47 +37,47 @@ func TestOSCMessageEncoding(t *testing.T) {
}, },
{ {
description: "simple address integer arg", description: "simple address integer arg",
message: OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "i", Value: 35}}}, message: &OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "i", Value: 35}}},
expected: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 105, 0, 0, 0, 0, 0, 35}, expected: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 105, 0, 0, 0, 0, 0, 35},
}, },
{ {
description: "simple address float arg", description: "simple address float arg",
message: OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "f", Value: 34.5}}}, message: &OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "f", Value: 34.5}}},
expected: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 102, 0, 0, 66, 10, 0, 0}, expected: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 102, 0, 0, 66, 10, 0, 0},
}, },
{ {
description: "simple address blob arg", description: "simple address blob arg",
message: OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "b", Value: []byte{98, 108, 111, 98}}}}, message: &OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "b", Value: []byte{98, 108, 111, 98}}}},
expected: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 98, 0, 0, 0, 0, 0, 4, 98, 108, 111, 98}, expected: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 98, 0, 0, 0, 0, 0, 4, 98, 108, 111, 98},
}, },
{ {
description: "simple address True arg", description: "simple address True arg",
message: OSCMessage{Address: "/hello", Args: []OSCArg{OSCArg{Type: "T", Value: true}}}, message: &OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "T", Value: true}}},
expected: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 84, 0, 0}, expected: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 84, 0, 0},
}, },
{ {
description: "simple address False arg", description: "simple address False arg",
message: OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "F", Value: false}}}, message: &OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "F", Value: false}}},
expected: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 70, 0, 0}, expected: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 70, 0, 0},
}, },
{ {
description: "simple address color arg", description: "simple address color arg",
message: OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "r", Value: OSCColor{r: 20, g: 21, b: 22, a: 10}}}}, message: &OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "r", Value: OSCColor{r: 20, g: 21, b: 22, a: 10}}}},
expected: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 114, 0, 0, 20, 21, 22, 10}, expected: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 114, 0, 0, 20, 21, 22, 10},
}, },
{ {
description: "simple address nil arg", description: "simple address nil arg",
message: OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "N", Value: nil}}}, message: &OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "N", Value: nil}}},
expected: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 78, 0, 0}, expected: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 78, 0, 0},
}, },
{ {
description: "simple address int64 arg", description: "simple address int64 arg",
message: OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "h", Value: 281474976710655}}}, message: &OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "h", Value: 281474976710655}}},
expected: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 104, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255}, expected: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 104, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255},
}, },
{ {
description: "simple address float64 arg", description: "simple address float64 arg",
message: OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "d", Value: 12.7654763}}}, message: &OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "d", Value: 12.7654763}}},
expected: []byte{ expected: []byte{
47, 104, 101, 108, 108, 111, 0, 0, 44, 100, 0, 0, 0x40, 0x29, 0x87, 0xec, 0x82, 0x74, 0xb9, 0xe6, 47, 104, 101, 108, 108, 111, 0, 0, 44, 100, 0, 0, 0x40, 0x29, 0x87, 0xec, 0x82, 0x74, 0xb9, 0xe6,
}, },
@@ -101,7 +101,7 @@ func TestOSCMessageEncoding(t *testing.T) {
// }, // },
{ {
description: "osc 1.0 spec example 1", description: "osc 1.0 spec example 1",
message: OSCMessage{Address: "/oscillator/4/frequency", Args: []OSCArg{{Type: "f", Value: 440}}}, message: &OSCMessage{Address: "/oscillator/4/frequency", Args: []OSCArg{{Type: "f", Value: 440}}},
expected: []byte{ expected: []byte{
47, 111, 115, 99, 105, 108, 108, 97, 116, 111, 114, 47, 52, 47, 102, 114, 101, 113, 117, 101, 110, 99, 121, 0, 44, 47, 111, 115, 99, 105, 108, 108, 97, 116, 111, 114, 47, 52, 47, 102, 114, 101, 113, 117, 101, 110, 99, 121, 0, 44,
102, 0, 0, 67, 220, 0, 0, 102, 0, 0, 67, 220, 0, 0,
@@ -109,7 +109,7 @@ func TestOSCMessageEncoding(t *testing.T) {
}, },
{ {
description: "osc 1.0 spec example 2", description: "osc 1.0 spec example 2",
message: OSCMessage{ message: &OSCMessage{
Address: "/foo", Address: "/foo",
Args: []OSCArg{ Args: []OSCArg{
{Type: "i", Value: 1000}, {Type: "i", Value: 1000},