Documentation
¶
Overview ¶
Package diff computes structured differences between two sorted record collections, keeping per-type statistics and printable summaries.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Added ¶
type Added bool
Added means an element was added, i.e. appears in a new set but not in the original.
type Change ¶
type Change struct {
// Added is the number of added elements (exist in b but not in a)
Added uint64
// Removed is the number of removed elements (exist in a but not in b)
Removed uint64
// Total is the total number of elements; added, removed or unchanged
Total uint64
}
Change is a count of element changes in a diff.
func (Change) LogValue ¶
LogValue returns a slog.Value representing the change as a group of attributes.
type Comparable ¶
Comparable is something that can be compared to something else and return an int indicating the order.
type Diff ¶
type Diff[C Comparable[C], T cmp.Ordered] struct { Lines []Line[C] Types[T] }
Diff is the difference between two zones.
Example ¶
package main
import (
"context"
"fmt"
"os"
"strings"
"gitlab.com/internetstiftelsen-oss/dnse/record"
"gitlab.com/internetstiftelsen-oss/dnse/zone"
"gitlab.com/internetstiftelsen-oss/dnse/zone/diff"
)
func main() {
ctx := context.Background()
a, b := readRecords(ctx, `
example.com. 3600 IN NS a.ns.example.com.
example.com. 3600 IN NS b.ns.example.com.
example.com. 86400 IN MX 0 .
example.com. 86400 IN TXT "foo"
`), readRecords(ctx, `
example.com. 86400 IN TXT "bar"
example.com. 86400 IN MX 0 .
example.com. 3600 IN A 198.51.100.42
example.com. 3600 IN AAAA 2001:db8::dead:beef
`)
a.Sort(ctx)
b.Sort(ctx)
d, err := diff.New(ctx, a, b, func(_ context.Context, r *record.Record) zone.ClassType {
return zone.NewClassType(r.Class(), r.Type())
})
if err != nil {
panic(err)
}
fmt.Println("diff:")
d.Print(ctx, os.Stdout)
fmt.Println("summary:")
fmt.Println(d)
}
func readRecords(ctx context.Context, str string) (records record.Records) {
for rr, err := range zone.Read(ctx, strings.NewReader(str), nil, "") {
if err != nil {
panic(err)
}
records = append(records, record.New(rr))
}
return records
}
Output: diff: + example.com. 3600 IN A 198.51.100.42 - example.com. 3600 IN NS a.ns.example.com. - example.com. 3600 IN NS b.ns.example.com. + example.com. 86400 IN TXT "bar" - example.com. 86400 IN TXT "foo" + example.com. 3600 IN AAAA 2001:db8::dead:beef summary: IN A: 1.000 (+1-0/1) IN NS: 1.000 (+0-2/2) IN MX: 0.000 (+0-0/1) IN TXT: 1.000 (+1-1/2) IN AAAA: 1.000 (+1-0/1) Total: 0.857 (+3-3/7)
func New ¶
func New[C Comparable[C], T cmp.Ordered]( ctx context.Context, sliceA, sliceB []C, typeFunc func(context.Context, C) T, ) (*Diff[C, T], error)
New compares two Comparable slices that must already be sorted.
func (*Diff[C, T]) LogValue ¶
LogValue passes to Types.LogValue.
type Line ¶
type Line[C Comparable[C]] struct { Added OrigLine, NewLine int C C }
Line is an added or removed element.
type Max ¶
type Max struct {
// contains filtered or unexported fields
}
Max is an inclusive limit for a change in elements. It‘s either a Rat or a uint64.
func MaxFromUint64 ¶
MaxFromUint64 creates a new Max with a uint64.
func (Max) LogValue ¶
LogValue returns a log/slog.Value for a Max: A string for a Rat or a uint64 for a uint64.
func (*Max) MarshalYAML ¶
MarshalYAML encodes the Max‘s inner value, the Rat or uint64, to YAML.
func (*Max) OK ¶
OK returns true if a change is below the Max. If the Max is a Rat then the change’s ratio is compared to it. If it is a uint64 then the total change is compared.
func (*Max) UnmarshalText ¶
UnmarshalText parses a Max from text. It only allows the ‘N/N’ format.
func (*Max) UnmarshalYAML ¶
UnmarshalYAML decodes a YAML node into a Max. A float is decoded into a Rat, an integer into a uint64 and a string uses Max.UnmarshalText. Any other type results in an error.
type Rat ¶
Rat wraps big.Rat with some sensible text conversion.
func (*Rat) LogValue ¶
LogValue returns a float64 slog.Value of the Rat.
func (*Rat) MarshalText ¶
MarshalText implements encoding.TextMarshaler using big.Rat.RatString.
func (*Rat) UnmarshalText ¶
UnmarshalText implements encoding.TextUnmarshaler by delegating to big.Rat.UnmarshalText.
type Types ¶
Types describes the differences between two slices grouped by their type, as well as the total difference.
func (*Types[T]) Each ¶
Each calls a function with each typed change as long as the function returns true.
func (*Types[T]) LogValue ¶
LogValue returns a group slog.Value with all the types and changes as well as the total.