Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions schema/bool.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ func (*Bool) Type() ValueType {
return TypeBool
}

func (*Bool) Size() int {
return 1
}

func (dst *Bool) Equal(src CQType) bool {
if src == nil {
return false
Expand Down
4 changes: 4 additions & 0 deletions schema/bytea.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ func (*Bytea) Type() ValueType {
return TypeByteArray
}

func (dst *Bytea) Size() int {
return len(dst.Bytes)
}

func (dst *Bytea) GetStatus() Status {
return dst.Status
}
Expand Down
26 changes: 26 additions & 0 deletions schema/bytea_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,29 @@ func TestByteaSet(t *testing.T) {
}
}
}

func TestBytea_Size(t *testing.T) {
tests := []struct {
name string
b Bytea
want int
}{
{
name: "present",
b: Bytea{Bytes: []byte{1, 2, 3}, Status: Present},
want: 3,
},
{
name: "null",
b: Bytea{Status: Null},
want: 0,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.b.Size(); got != tt.want {
t.Errorf("Bytea.Size() = %v, want %v", got, tt.want)
}
})
}
}
4 changes: 4 additions & 0 deletions schema/cidr.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ func (*CIDR) Type() ValueType {
return TypeCIDR
}

func (dst *CIDR) Size() int {
return len(dst.IPNet.IP) + len(dst.IPNet.Mask)
}

func (dst *CIDR) String() string {
if dst.Status == Present {
return dst.IPNet.String()
Expand Down
8 changes: 8 additions & 0 deletions schema/cidr_array.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ func (*CIDRArray) Type() ValueType {
return TypeCIDRArray
}

func (dst *CIDRArray) Size() int {
totalSize := 0
for _, element := range dst.Elements {
totalSize += element.Size()
}
return totalSize
}

func (dst *CIDRArray) Equal(src CQType) bool {
if src == nil {
return false
Expand Down
11 changes: 11 additions & 0 deletions schema/cidr_array_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,14 @@ func TestCIDRArraySet(t *testing.T) {
}
}
}

func TestCIDRArray_Size(t *testing.T) {
a := CIDRArray{
Elements: []CIDR{{IPNet: mustParseCIDR(t, "127.0.0.1/32"), Status: Present}},
Dimensions: []ArrayDimension{{LowerBound: 1, Length: 1}},
Status: Present,
}
if a.Size() != 8 {
t.Errorf("Size() = %d, want 8", a.Size())
}
}
4 changes: 4 additions & 0 deletions schema/float8.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ func (*Float8) Type() ValueType {
return TypeFloat
}

func (dst *Float8) Size() int {
return 8
}

func (dst *Float8) String() string {
if dst.Status == Present {
return strconv.FormatFloat(dst.Float, 'f', -1, 64)
Expand Down
26 changes: 26 additions & 0 deletions schema/float8_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,29 @@ func TestFloat8Set(t *testing.T) {
}
}
}

func TestFloat8_Size(t *testing.T) {
tests := []struct {
name string
f Float8
want int
}{
{
name: "present",
f: Float8{Float: 1, Status: Present},
want: 8,
},
{
name: "null",
f: Float8{Status: Null},
want: 8,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.f.Size(); got != tt.want {
t.Errorf("Float8.Size() = %v, want %v", got, tt.want)
}
})
}
}
3 changes: 3 additions & 0 deletions schema/inet.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ func (*Inet) Type() ValueType {
return TypeInet
}

func (dst *Inet) Size() int {
return len(dst.IPNet.IP) + len(dst.IPNet.Mask)
}
func (dst *Inet) GetStatus() Status {
return dst.Status
}
Expand Down
8 changes: 8 additions & 0 deletions schema/inet_array.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ func (*InetArray) Type() ValueType {
return TypeInetArray
}

func (dst *InetArray) Size() int {
totalSize := 0
for _, element := range dst.Elements {
totalSize += element.Size()
}
return totalSize
}

func (dst *InetArray) Equal(src CQType) bool {
if src == nil {
return false
Expand Down
26 changes: 26 additions & 0 deletions schema/inet_array_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,29 @@ func TestInetArraySet(t *testing.T) {
}
}
}

func TestInetArray_Size(t *testing.T) {
tests := []struct {
source InetArray
want int
}{
{
source: InetArray{
Elements: []Inet{
{IPNet: mustParseCIDR(t, "127.0.0.1/24"), Status: Present},
{IPNet: mustParseCIDR(t, "10.0.0.1/24"), Status: Present},
},
Dimensions: []ArrayDimension{
{LowerBound: 1, Length: 2},
{LowerBound: 1, Length: 1}},
Status: Present,
},
want: 16,
},
}
for _, tt := range tests {
if tt.source.Size() != tt.want {
t.Errorf("%v.Size() = %d, want %v", tt.source, tt.source.Size(), tt.want)
}
}
}
4 changes: 4 additions & 0 deletions schema/int8.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ func (*Int8) Type() ValueType {
return TypeInt
}

func (*Int8) Size() int {
return 8
}

func (dst *Int8) Equal(src CQType) bool {
if src == nil {
return false
Expand Down
4 changes: 4 additions & 0 deletions schema/int8_array.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ func (*Int8Array) Type() ValueType {
return TypeIntArray
}

func (dst *Int8Array) Size() int {
return 8 * len(dst.Elements)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we call Int8.Size() here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this one is probably okay, the name even has 8 in it, and we're unlikely to redefine the size of 64-bit ints :) Otherwise we have to write:

if len(dst.Elements) == 0 {
     return 0
}
return dst.Elements[0] * len(dst.Elements)

And then there's still the assumption that all the elements have the same size.

We could also do:

	totalSize := 0
	for _, element := range dst.Elements {
		totalSize += element.Size()
	}
	return totalSize

but that would be unnecessarily inefficient, being O(N) instead of O(1)

}

func (dst *Int8Array) Equal(src CQType) bool {
if src == nil {
return false
Expand Down
16 changes: 16 additions & 0 deletions schema/int8_array_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,19 @@ func TestInt8ArraySet(t *testing.T) {
}
}
}

func TestInt8Array_Size(t *testing.T) {
var r Int8Array
var sz int

sz = r.Size()
if sz != 0 {
t.Errorf("%v.Size() = %d, want %d", r, sz, 0)
}

_ = r.Set([]int64{1, 2, 3})
sz = r.Size()
if sz != 24 {
t.Errorf("%v.Size() = %d, want %d", r, sz, 24)
}
}
4 changes: 4 additions & 0 deletions schema/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ func (*JSON) Type() ValueType {
return TypeJSON
}

func (dst *JSON) Size() int {
return len(dst.Bytes)
}

// JSONBytesEqual compares the JSON in two byte slices.
func jsonBytesEqual(a, b []byte) (bool, error) {
var j, j2 any
Expand Down
26 changes: 26 additions & 0 deletions schema/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,29 @@ func TestJSONSet(t *testing.T) {
}
}
}

func TestJSON_Size(t *testing.T) {
tests := []struct {
name string
j JSON
want int
}{
{
name: "empty",
j: JSON{Status: Null},
want: 0,
},
{
name: "present",
j: JSON{Bytes: []byte(`{"foo":"bar"}`), Status: Present},
want: 13,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.j.Size(); got != tt.want {
t.Errorf("JSON.Size() = %v, want %v", got, tt.want)
}
})
}
}
4 changes: 4 additions & 0 deletions schema/macaddr.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ func (*Macaddr) Type() ValueType {
return TypeMacAddr
}

func (dst *Macaddr) Size() int {
return len(dst.Addr)
}

func (dst *Macaddr) Equal(src CQType) bool {
if src == nil {
return false
Expand Down
8 changes: 8 additions & 0 deletions schema/macaddr_array.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ func (*MacaddrArray) Type() ValueType {
return TypeMacAddrArray
}

func (dst *MacaddrArray) Size() int {
totalSize := 0
for _, element := range dst.Elements {
totalSize += element.Size()
}
return totalSize
}

func (dst *MacaddrArray) Equal(src CQType) bool {
if src == nil {
return false
Expand Down
26 changes: 26 additions & 0 deletions schema/macaddr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,29 @@ func TestMacaddrSet(t *testing.T) {
}
}
}

func TestMacaddr_Size(t *testing.T) {
tests := []struct {
name string
b Macaddr
want int
}{
{
name: "present",
b: Macaddr{Addr: mustParseMacaddr(t, "01:23:45:67:89:ab"), Status: Present},
want: 6,
},
{
name: "null",
b: Macaddr{Status: Null},
want: 0,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.b.Size(); got != tt.want {
t.Errorf("Macaddr.Size() = %v, want %v", got, tt.want)
}
})
}
}
4 changes: 4 additions & 0 deletions schema/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ func (*Text) Type() ValueType {
return TypeString
}

func (dst *Text) Size() int {
return len(dst.Str)
}

func (dst *Text) Equal(src CQType) bool {
if src == nil {
return false
Expand Down
8 changes: 8 additions & 0 deletions schema/text_array.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ func (*TextArray) Type() ValueType {
return TypeStringArray
}

func (dst *TextArray) Size() int {
totalSize := 0
for _, element := range dst.Elements {
totalSize += element.Size()
}
return totalSize
}

func (dst *TextArray) Equal(src CQType) bool {
if src == nil {
return false
Expand Down
Loading