Created
February 19, 2021 19:42
-
-
Save rnkoaa/ea2ad77356ea67bd4d00fa6bee01ffcf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"net" | |
"strings" | |
"time" | |
"github.com/fatih/color" | |
"github.com/google/go-cmp/cmp" | |
"github.com/google/go-cmp/cmp/cmpopts" | |
) | |
type ( | |
Gateway struct { | |
SSID string | |
IPAddress net.IP | |
NetMask net.IPMask | |
Clients []Client | |
} | |
Client struct { | |
Hostname string | |
IPAddress net.IP | |
LastSeen time.Time | |
} | |
) | |
// StructDiffReporter is a simple custom reporter that only records differences | |
// detected during comparison. | |
type StructDiffReporter struct { | |
path cmp.Path | |
diffs []string | |
} | |
func (r *StructDiffReporter) PushStep(ps cmp.PathStep) { | |
r.path = append(r.path, ps) | |
} | |
func (r *StructDiffReporter) Report(rs cmp.Result) { | |
if !rs.Equal() { | |
vx, vy := r.path.Last().Values() | |
missing := fmt.Sprintf("- %+v", vx) | |
found := fmt.Sprintf("+ %+v", vy) | |
r.diffs = append(r.diffs, fmt.Sprintf("%#v\n\t%s\n\t%s\n", r.path, color.RedString(missing), color.GreenString(found))) | |
} | |
} | |
func (r *StructDiffReporter) PopStep() { | |
r.path = r.path[:len(r.path)-1] | |
} | |
func (r *StructDiffReporter) String() string { | |
return strings.Join(r.diffs, "\n") | |
} | |
// StringDiffReporter is a simple custom reporter that only records differences | |
// detected during comparison. | |
type StringDiffReporter struct { | |
path cmp.Path | |
diffs []string | |
} | |
func (r *StringDiffReporter) PushStep(ps cmp.PathStep) { | |
r.path = append(r.path, ps) | |
} | |
func (r *StringDiffReporter) Report(rs cmp.Result) { | |
if !rs.Equal() { | |
vx, vy := r.path.Last().Values() | |
missing := fmt.Sprintf("- %+v", vx) | |
found := fmt.Sprintf("+ %+v", vy) | |
r.diffs = append(r.diffs, fmt.Sprintf("%s\n%s\n", color.RedString(missing), color.GreenString(found))) | |
} | |
} | |
func (r *StringDiffReporter) PopStep() { | |
r.path = r.path[:len(r.path)-1] | |
} | |
func (r *StringDiffReporter) String() string { | |
return strings.Join(r.diffs, "\n") | |
} | |
func main() { | |
/** | |
// While the specified fields will be semantically ignored for the comparison, | |
// the fields may be printed in the diff when displaying entire values | |
// that are already determined to be different. | |
if diff := cmp.Diff(want, got, cmpopts.IgnoreFields(Client{}, "IPAddress")); diff != "" { | |
lines := strings.Split(diff, "\n") | |
changes := "" | |
for _, line := range lines { | |
if strings.HasPrefix(line, "+") || strings.HasPrefix(line, "-") { | |
changes += line + "\n" | |
} | |
} | |
fmt.Println(changes) | |
t.Errorf("MakeGatewayInfo() mismatch (-want +got):\n%s", diff) | |
} | |
**/ | |
got, want := MakeGatewayInfo() | |
var r StructDiffReporter | |
cmp.Equal(got, want, cmpopts.IgnoreFields(Gateway{}, "IPAddress"), cmpopts.IgnoreFields(Client{}, "IPAddress"), cmp.Reporter(&r)) | |
if r.String() != "" { | |
fmt.Println(r.String()) | |
return | |
} | |
fmt.Println("Equal") | |
} | |
func MakeGatewayInfo() (x, y Gateway) { | |
x = Gateway{ | |
SSID: "CoffeeShopWiFi", | |
IPAddress: net.IPv4(192, 168, 0, 1), | |
NetMask: net.IPv4Mask(255, 255, 0, 0), | |
Clients: []Client{{ | |
Hostname: "ristretto", | |
IPAddress: net.IPv4(192, 168, 0, 116), | |
}, { | |
Hostname: "aribica", | |
IPAddress: net.IPv4(192, 168, 0, 104), | |
LastSeen: time.Date(2009, time.November, 10, 23, 6, 32, 0, time.UTC), | |
}, { | |
Hostname: "macchiato", | |
IPAddress: net.IPv4(192, 168, 0, 153), | |
LastSeen: time.Date(2009, time.November, 10, 23, 39, 43, 0, time.UTC), | |
}, { | |
Hostname: "espresso", | |
IPAddress: net.IPv4(192, 168, 0, 121), | |
}, { | |
Hostname: "latte", | |
IPAddress: net.IPv4(192, 168, 0, 219), | |
// IPAddress: net.IPv4(192, 168, 0, 221), | |
LastSeen: time.Date(2009, time.November, 10, 23, 0, 23, 0, time.UTC), | |
}, | |
// { | |
// Hostname: "americano", | |
// IPAddress: net.IPv4(192, 168, 0, 188), | |
// LastSeen: time.Date(2009, time.November, 10, 23, 3, 5, 0, time.UTC), | |
// }, | |
}, | |
} | |
y = Gateway{ | |
SSID: "CoffeeShopWiFi", | |
IPAddress: net.IPv4(192, 168, 0, 2), | |
NetMask: net.IPv4Mask(255, 255, 0, 0), | |
Clients: []Client{{ | |
Hostname: "ristretto", | |
IPAddress: net.IPv4(192, 168, 0, 116), | |
}, { | |
Hostname: "aribica", | |
IPAddress: net.IPv4(192, 168, 0, 104), | |
LastSeen: time.Date(2009, time.November, 10, 23, 6, 32, 0, time.UTC), | |
}, { | |
Hostname: "macchiato", | |
IPAddress: net.IPv4(192, 168, 0, 153), | |
LastSeen: time.Date(2009, time.November, 10, 23, 39, 43, 0, time.UTC), | |
}, { | |
Hostname: "espresso", | |
IPAddress: net.IPv4(192, 168, 0, 121), | |
}, { | |
Hostname: "latte", | |
IPAddress: net.IPv4(192, 168, 0, 221), | |
LastSeen: time.Date(2009, time.November, 10, 23, 0, 23, 0, time.UTC), | |
}}, | |
} | |
return x, y | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment