Skip to content

Commit

Permalink
fix: logic mergeFields
Browse files Browse the repository at this point in the history
  • Loading branch information
nekrassov01 committed Feb 19, 2024
1 parent 8a8cfa4 commit 607dca5
Show file tree
Hide file tree
Showing 8 changed files with 163 additions and 156 deletions.
Binary file modified _assets/backlog_merged.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified _assets/markdown_merged.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified _assets/text.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified _assets/text_compressed.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified _assets/text_merged.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
207 changes: 104 additions & 103 deletions _examples/main.go

Large diffs are not rendered by default.

13 changes: 9 additions & 4 deletions mintab.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ func (t *Table) setData(v reflect.Value) error {
if field.Kind() == reflect.Ptr {
field = field.Elem()
}
merge := true
for j, h := range t.header {
field := field.FieldByName(h)
if !field.IsValid() {
Expand All @@ -337,10 +338,14 @@ func (t *Table) setData(v reflect.Value) error {
if err != nil {
return fmt.Errorf("failed to format field \"%s\": %w", h, err)
}
if slices.Contains(t.mergedFields, j) && f == prev[j] {
f = ""
} else {
prev[j] = f
if slices.Contains(t.mergedFields, j) {
if f != prev[j] {
merge = false
prev[j] = f
}
if merge {
f = ""
}
}
row[j] = f
for _, line := range strings.Split(f, "\n") {
Expand Down
99 changes: 50 additions & 49 deletions mintab_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type nestedSample struct {
type mergedSample struct {
InstanceID string
InstanceName string
VPCID string
SecurityGroupID string
FlowDirection string
IPProtocol string
Expand Down Expand Up @@ -98,14 +99,14 @@ func setup() {
},
}
mergedsample = []mergedSample{
{InstanceID: "i-1", InstanceName: "server-1", SecurityGroupID: "sg-1", FlowDirection: "Ingress", IPProtocol: "tcp", FromPort: 22, ToPort: 22, AddressType: "SecurityGroup", CidrBlock: "sg-10"},
{InstanceID: "i-1", InstanceName: "server-1", SecurityGroupID: "sg-1", FlowDirection: "Egress", IPProtocol: "-1", FromPort: 0, ToPort: 0, AddressType: "Ipv4", CidrBlock: "0.0.0.0/0"},
{InstanceID: "i-1", InstanceName: "server-1", SecurityGroupID: "sg-2", FlowDirection: "Ingress", IPProtocol: "tcp", FromPort: 443, ToPort: 443, AddressType: "Ipv4", CidrBlock: "0.0.0.0/0"},
{InstanceID: "i-1", InstanceName: "server-1", SecurityGroupID: "sg-2", FlowDirection: "Egress", IPProtocol: "-1", FromPort: 0, ToPort: 0, AddressType: "Ipv4", CidrBlock: "0.0.0.0/0"},
{InstanceID: "i-2", InstanceName: "server-2", SecurityGroupID: "sg-3", FlowDirection: "Ingress", IPProtocol: "icmp", FromPort: -1, ToPort: -1, AddressType: "SecurityGroup", CidrBlock: "sg-11"},
{InstanceID: "i-2", InstanceName: "server-2", SecurityGroupID: "sg-3", FlowDirection: "Ingress", IPProtocol: "tcp", FromPort: 3389, ToPort: 3389, AddressType: "Ipv4", CidrBlock: "10.1.0.0/16"},
{InstanceID: "i-2", InstanceName: "server-2", SecurityGroupID: "sg-3", FlowDirection: "Ingress", IPProtocol: "tcp", FromPort: 0, ToPort: 65535, AddressType: "PrefixList", CidrBlock: "pl-id/pl-name"},
{InstanceID: "i-2", InstanceName: "server-2", SecurityGroupID: "sg-3", FlowDirection: "Egress", IPProtocol: "-1", FromPort: 0, ToPort: 0, AddressType: "Ipv4", CidrBlock: "0.0.0.0/0"},
{InstanceID: "i-1", InstanceName: "server-1", VPCID: "vpc-1", SecurityGroupID: "sg-1", FlowDirection: "Ingress", IPProtocol: "tcp", FromPort: 22, ToPort: 22, AddressType: "SecurityGroup", CidrBlock: "sg-10"},
{InstanceID: "i-1", InstanceName: "server-1", VPCID: "vpc-1", SecurityGroupID: "sg-1", FlowDirection: "Egress", IPProtocol: "-1", FromPort: 0, ToPort: 0, AddressType: "Ipv4", CidrBlock: "0.0.0.0/0"},
{InstanceID: "i-1", InstanceName: "server-1", VPCID: "vpc-1", SecurityGroupID: "sg-2", FlowDirection: "Ingress", IPProtocol: "tcp", FromPort: 443, ToPort: 443, AddressType: "Ipv4", CidrBlock: "0.0.0.0/0"},
{InstanceID: "i-1", InstanceName: "server-1", VPCID: "vpc-1", SecurityGroupID: "sg-2", FlowDirection: "Egress", IPProtocol: "-1", FromPort: 0, ToPort: 0, AddressType: "Ipv4", CidrBlock: "0.0.0.0/0"},
{InstanceID: "i-2", InstanceName: "server-2", VPCID: "vpc-1", SecurityGroupID: "sg-3", FlowDirection: "Ingress", IPProtocol: "icmp", FromPort: -1, ToPort: -1, AddressType: "SecurityGroup", CidrBlock: "sg-11"},
{InstanceID: "i-2", InstanceName: "server-2", VPCID: "vpc-1", SecurityGroupID: "sg-3", FlowDirection: "Ingress", IPProtocol: "tcp", FromPort: 3389, ToPort: 3389, AddressType: "Ipv4", CidrBlock: "10.1.0.0/16"},
{InstanceID: "i-2", InstanceName: "server-2", VPCID: "vpc-1", SecurityGroupID: "sg-3", FlowDirection: "Ingress", IPProtocol: "tcp", FromPort: 0, ToPort: 65535, AddressType: "PrefixList", CidrBlock: "pl-id/pl-name"},
{InstanceID: "i-2", InstanceName: "server-2", VPCID: "vpc-1", SecurityGroupID: "sg-3", FlowDirection: "Egress", IPProtocol: "-1", FromPort: 0, ToPort: 0, AddressType: "Ipv4", CidrBlock: "0.0.0.0/0"},
}
basicsamplePtr = make([]*basicSample, 0, len(basicsample))
for i := range basicsample {
Expand Down Expand Up @@ -401,33 +402,33 @@ func TestTable_Out(t *testing.T) {
name: "text_with_compressed",
fields: fields{
format: FormatText,
header: []string{"InstanceID", "InstanceName", "SecurityGroupID", "FlowDirection", "IPProtocol", "FromPort", "ToPort", "AddressType", "CidrBlock"},
header: []string{"InstanceID", "InstanceName", "VPCID", "SecurityGroupID", "FlowDirection", "IPProtocol", "FromPort", "ToPort", "AddressType", "CidrBlock"},
data: [][]string{
{"i-1", "server-1", "sg-1", "Ingress", "tcp", "22", "22", "SecurityGroup", "sg-10"},
{"", "", "", "Egress", "-1", "0", "0", "Ipv4", "0.0.0.0/0"},
{"", "", "sg-2", "Ingress", "tcp", "443", "443", "Ipv4", "0.0.0.0/0"},
{"", "", "", "Egress", "-1", "0", "0", "Ipv4", "0.0.0.0/0"},
{"i-2", "server-2", "sg-3", "Ingress", "icmp", "-1", "-1", "SecurityGroup", "sg-11"},
{"", "", "", "Ingress", "tcp", "3389", "3389", "Ipv4", "10.1.0.0/16"},
{"", "", "", "Ingress", "tcp", "0", "65535", "PrefixList", "pl-id/pl-name"},
{"", "", "", "Egress", "-1", "0", "0", "Ipv4", "0.0.0.0/0"},
{"i-1", "server-1", "vpc-1", "sg-1", "Ingress", "tcp", "22", "22", "SecurityGroup", "sg-10"},
{"", "", "", "", "Egress", "-1", "0", "0", "Ipv4", "0.0.0.0/0"},
{"", "", "", "sg-2", "Ingress", "tcp", "443", "443", "Ipv4", "0.0.0.0/0"},
{"", "", "", "", "Egress", "-1", "0", "0", "Ipv4", "0.0.0.0/0"},
{"i-2", "server-2", "vpc-1", "sg-3", "Ingress", "icmp", "-1", "-1", "SecurityGroup", "sg-11"},
{"", "", "", "", "Ingress", "tcp", "3389", "3389", "Ipv4", "10.1.0.0/16"},
{"", "", "", "", "Ingress", "tcp", "0", "65535", "PrefixList", "pl-id/pl-name"},
{"", "", "", "", "Egress", "-1", "0", "0", "Ipv4", "0.0.0.0/0"},
},
columnWidths: []int{10, 12, 15, 13, 10, 8, 6, 13, 13},
columnWidths: []int{10, 12, 5, 15, 13, 10, 8, 6, 13, 13},
compress: true,
},
want: `+------------+--------------+-----------------+---------------+------------+----------+--------+---------------+---------------+
| InstanceID | InstanceName | SecurityGroupID | FlowDirection | IPProtocol | FromPort | ToPort | AddressType | CidrBlock |
+------------+--------------+-----------------+---------------+------------+----------+--------+---------------+---------------+
| i-1 | server-1 | sg-1 | Ingress | tcp | 22 | 22 | SecurityGroup | sg-10 |
| | | | Egress | -1 | 0 | 0 | Ipv4 | 0.0.0.0/0 |
| | | sg-2 | Ingress | tcp | 443 | 443 | Ipv4 | 0.0.0.0/0 |
| | | | Egress | -1 | 0 | 0 | Ipv4 | 0.0.0.0/0 |
+------------+--------------+-----------------+---------------+------------+----------+--------+---------------+---------------+
| i-2 | server-2 | sg-3 | Ingress | icmp | -1 | -1 | SecurityGroup | sg-11 |
| | | | Ingress | tcp | 3389 | 3389 | Ipv4 | 10.1.0.0/16 |
| | | | Ingress | tcp | 0 | 65535 | PrefixList | pl-id/pl-name |
| | | | Egress | -1 | 0 | 0 | Ipv4 | 0.0.0.0/0 |
+------------+--------------+-----------------+---------------+------------+----------+--------+---------------+---------------+
want: `+------------+--------------+-------+-----------------+---------------+------------+----------+--------+---------------+---------------+
| InstanceID | InstanceName | VPCID | SecurityGroupID | FlowDirection | IPProtocol | FromPort | ToPort | AddressType | CidrBlock |
+------------+--------------+-------+-----------------+---------------+------------+----------+--------+---------------+---------------+
| i-1 | server-1 | vpc-1 | sg-1 | Ingress | tcp | 22 | 22 | SecurityGroup | sg-10 |
| | | | | Egress | -1 | 0 | 0 | Ipv4 | 0.0.0.0/0 |
| | | | sg-2 | Ingress | tcp | 443 | 443 | Ipv4 | 0.0.0.0/0 |
| | | | | Egress | -1 | 0 | 0 | Ipv4 | 0.0.0.0/0 |
+------------+--------------+-------+-----------------+---------------+------------+----------+--------+---------------+---------------+
| i-2 | server-2 | vpc-1 | sg-3 | Ingress | icmp | -1 | -1 | SecurityGroup | sg-11 |
| | | | | Ingress | tcp | 3389 | 3389 | Ipv4 | 10.1.0.0/16 |
| | | | | Ingress | tcp | 0 | 65535 | PrefixList | pl-id/pl-name |
| | | | | Egress | -1 | 0 | 0 | Ipv4 | 0.0.0.0/0 |
+------------+--------------+-------+-----------------+---------------+------------+----------+--------+---------------+---------------+
`,
},
Expand Down Expand Up @@ -687,28 +688,28 @@ func TestTable_printData(t *testing.T) {
name: "text_with_compress",
fields: fields{
data: [][]string{
{"i-1", "server-1", "sg-1", "Ingress", "tcp", "22", "22", "SecurityGroup", "sg-10"},
{"", "", "", "Egress", "-1", "0", "0", "Ipv4", "0.0.0.0/0"},
{"", "", "sg-2", "Ingress", "tcp", "443", "443", "Ipv4", "0.0.0.0/0"},
{"", "", "", "Egress", "-1", "0", "0", "Ipv4", "0.0.0.0/0"},
{"i-2", "server-2", "sg-3", "Ingress", "icmp", "-1", "-1", "SecurityGroup", "sg-11"},
{"", "", "", "Ingress", "tcp", "3389", "3389", "Ipv4", "10.1.0.0/16"},
{"", "", "", "Ingress", "tcp", "0", "65535", "PrefixList", "pl-id/pl-name"},
{"", "", "", "Egress", "-1", "0", "0", "Ipv4", "0.0.0.0/0"},
{"i-1", "server-1", "vpc-1", "sg-1", "Ingress", "tcp", "22", "22", "SecurityGroup", "sg-10"},
{"", "", "", "", "Egress", "-1", "0", "0", "Ipv4", "0.0.0.0/0"},
{"", "", "", "sg-2", "Ingress", "tcp", "443", "443", "Ipv4", "0.0.0.0/0"},
{"", "", "", "", "Egress", "-1", "0", "0", "Ipv4", "0.0.0.0/0"},
{"i-2", "server-2", "vpc-1", "sg-3", "Ingress", "icmp", "-1", "-1", "SecurityGroup", "sg-11"},
{"", "", "", "", "Ingress", "tcp", "3389", "3389", "Ipv4", "10.1.0.0/16"},
{"", "", "", "", "Ingress", "tcp", "0", "65535", "PrefixList", "pl-id/pl-name"},
{"", "", "", "", "Egress", "-1", "0", "0", "Ipv4", "0.0.0.0/0"},
},
format: FormatText,
columnWidths: []int{10, 12, 15, 13, 10, 8, 6, 13, 13},
columnWidths: []int{10, 12, 5, 15, 13, 10, 8, 6, 13, 13},
compress: true,
},
want: `| i-1 | server-1 | sg-1 | Ingress | tcp | 22 | 22 | SecurityGroup | sg-10 |
| | | | Egress | -1 | 0 | 0 | Ipv4 | 0.0.0.0/0 |
| | | sg-2 | Ingress | tcp | 443 | 443 | Ipv4 | 0.0.0.0/0 |
| | | | Egress | -1 | 0 | 0 | Ipv4 | 0.0.0.0/0 |
+------------+--------------+-----------------+---------------+------------+----------+--------+---------------+---------------+
| i-2 | server-2 | sg-3 | Ingress | icmp | -1 | -1 | SecurityGroup | sg-11 |
| | | | Ingress | tcp | 3389 | 3389 | Ipv4 | 10.1.0.0/16 |
| | | | Ingress | tcp | 0 | 65535 | PrefixList | pl-id/pl-name |
| | | | Egress | -1 | 0 | 0 | Ipv4 | 0.0.0.0/0 |
want: `| i-1 | server-1 | vpc-1 | sg-1 | Ingress | tcp | 22 | 22 | SecurityGroup | sg-10 |
| | | | | Egress | -1 | 0 | 0 | Ipv4 | 0.0.0.0/0 |
| | | | sg-2 | Ingress | tcp | 443 | 443 | Ipv4 | 0.0.0.0/0 |
| | | | | Egress | -1 | 0 | 0 | Ipv4 | 0.0.0.0/0 |
+------------+--------------+-------+-----------------+---------------+------------+----------+--------+---------------+---------------+
| i-2 | server-2 | vpc-1 | sg-3 | Ingress | icmp | -1 | -1 | SecurityGroup | sg-11 |
| | | | | Ingress | tcp | 3389 | 3389 | Ipv4 | 10.1.0.0/16 |
| | | | | Ingress | tcp | 0 | 65535 | PrefixList | pl-id/pl-name |
| | | | | Egress | -1 | 0 | 0 | Ipv4 | 0.0.0.0/0 |
`,
},
}
Expand Down

0 comments on commit 607dca5

Please sign in to comment.