Skip to content

Commit

Permalink
Add INET column type. Closes #22
Browse files Browse the repository at this point in the history
  • Loading branch information
kaidaguerre committed Feb 22, 2021
1 parent 31164f2 commit 9cbf9de
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 24 deletions.
43 changes: 23 additions & 20 deletions grpc/proto/plugin.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions grpc/proto/plugin.proto
Original file line number Diff line number Diff line change
Expand Up @@ -156,5 +156,6 @@ enum ColumnType {
IPADDR = 6;
CIDR = 7;
TIMESTAMP = 8;
INET = 9;
UNKNOWN = -1;
}
20 changes: 17 additions & 3 deletions plugin/table_column.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func (t *Table) interfaceToColumnValue(column *Column, val interface{}) (*proto.
break
case proto.ColumnType_IPADDR:
ipString := types.SafeString(val)
// tread an empty string as a null ip address
// treat an empty string as a null ip address
if ipString == "" {
columnValue = &proto.Column{Value: &proto.Column_NullValue{}}
} else {
Expand All @@ -172,16 +172,30 @@ func (t *Table) interfaceToColumnValue(column *Column, val interface{}) (*proto.
break
case proto.ColumnType_CIDR:
cidrRangeString := types.SafeString(val)
// tread an empty string as a null ip address
// treat an empty string as a null ip address
if cidrRangeString == "" {
columnValue = &proto.Column{Value: &proto.Column_NullValue{}}
} else {
if _, _, err := net.ParseCIDR(cidrRangeString); err != nil {
return nil, fmt.Errorf("interfaceToColumnValue failed for column '%s': %v", column.Name, err)
return nil, fmt.Errorf("%s: invalid ip address %s", column.Name, cidrRangeString)
}
columnValue = &proto.Column{Value: &proto.Column_CidrRangeValue{CidrRangeValue: cidrRangeString}}
}
break
case proto.ColumnType_INET:
inetString := types.SafeString(val)
// treat an empty string as a null ip address
if inetString == "" {
columnValue = &proto.Column{Value: &proto.Column_NullValue{}}
} else {
if ip := net.ParseIP(inetString); ip == nil {
if _, _, err := net.ParseCIDR(inetString); err != nil {
return nil, fmt.Errorf("%s: invalid ip address %s", column.Name, inetString)
}
}
columnValue = &proto.Column{Value: &proto.Column_CidrRangeValue{CidrRangeValue: inetString}}
}
break
default:
return nil, fmt.Errorf("unrecognised columnValue type '%s'", column.Type)
}
Expand Down
2 changes: 1 addition & 1 deletion plugin/table_quals.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func ColumnQualValue(qv *proto.QualValue, column *Column) interface{} {
return qv.GetTimestampValue()
case proto.ColumnType_JSON:
return qv.GetJsonbValue()
case proto.ColumnType_IPADDR, proto.ColumnType_CIDR:
case proto.ColumnType_IPADDR, proto.ColumnType_CIDR, proto.ColumnType_INET:
return qv.GetInetValue()
default:
return nil
Expand Down
2 changes: 2 additions & 0 deletions plugin/table_validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ func columnTypeToString(columnType proto.ColumnType) string {
return "ColumnType_IPADDR"
case proto.ColumnType_CIDR:
return "ColumnType_CIDR"
case proto.ColumnType_INET:
return "ColumnType_INET"
case proto.ColumnType_TIMESTAMP:
return "ColumnType_TIMESTAMP"
default:
Expand Down

0 comments on commit 9cbf9de

Please sign in to comment.