Dawnline 0.20.0
Build packages / CI (push) Blocked by required conditions
Build packages / Version (push) Waiting to run
CI / Web (format, types, tests, bundles) (push) Blocked by required conditions
CI / ShellCheck (push) Blocked by required conditions
CI / Backend (Go: vet, tests, a real ubusd and uhttpd) (push) Blocked by required conditions
CI / Package files (push) Blocked by required conditions
Build packages / CI (push) Blocked by required conditions
Build packages / Version (push) Waiting to run
CI / Web (format, types, tests, bundles) (push) Blocked by required conditions
CI / ShellCheck (push) Blocked by required conditions
CI / Backend (Go: vet, tests, a real ubusd and uhttpd) (push) Blocked by required conditions
CI / Package files (push) Blocked by required conditions
- **Backend rewritten in Go.** One daemon, `dawnlined`, replaces the whole ucode backend: the rpcd plugin, the agent, the CLI and the updater. The package no longer depends on ucode. - **Per-architecture builds.** The release carries the backend for arm64, ARMv7, mips, mipsle and amd64; the router fetches its own after the package is installed and checks its sha256. Dawnline does not run on other architectures (older ARM, 32-bit x86, RISC-V). - **Faster on the router.** A firewall sync takes 0.06 s instead of 0.9 s; a full apply takes about 300 ms. The portal is served by the daemon itself, without a CGI process per request. - **Live pages.** The admin and the portal follow a live stream from the router instead of re-fetching the whole state every 30 seconds. - **Admin redesigned in the portal's style.** Three sections (Overview · Log · Settings); a server is a page of its own with People, Places and Settings; a device opens as a dialog over its person. - **Changes wait for «Apply».** Every setting, and a person's access, is staged in one dock before it reaches the router. - **Configuration rollout.** After a change that makes devices fetch their configuration again, the admin shows how many have fetched it and who has not. Every setting shows beforehand (⟳N) how many devices it will touch. - **Waiting for a decision.** Overview and People put first the people who need one (access ended, limit reached, a device that does not connect), each with its one action a tap away. - **Bulk actions on people.** «Select» (or a long press on a card) picks several people to extend by a month, pause or resume at once. - **Undo.** An action done from a card or a request is undone from its toast. - **Update dialogs redesigned.** An update is one view from its question to its result; Dawnline's own update runs in it like every other, and the page reloads itself once the new version answers. Other versions are a list, each with its notes and Install on a page of its own. - **Site to site: networks behind a router.** A router connected as a device can have networks behind it (up to eight, /16 to /30): the server routes them and applies the router's access to them. A network that shares addresses with a tunnel, the router's own networks or another site is refused by name. - **Routers from the portal.** People can add a router and name its networks on their own page; the admin accepts or declines with a reason, or lets them through without asking. - **Check access and Health know the networks behind routers.** - **Usage limits reworked.** A monthly limit no longer follows the calendar. A person's month starts with their first traffic and ends a month later; the next one starts only when they connect again, so time away uses up none of it. Before, everyone's limit reset on the 1st. - Extra data lasts until that person's month ends; «Reset usage» starts the month anew. - A limit is for a month or for the whole term; the admin and the portal show when it resets. - Charts that said «this month» show the last 30 days. - After the update nobody loses what was counted: the month that was running becomes the person's first and ends on the 1st. - **Access by time of day removed.** The Hours limit is gone from the admin, the portal and the backend; saved hours are dropped. - **Expiry simplified.** An end date always stops access, with a heads-up three days before; the per-person choice is gone. - **No false disconnects from sleeping phones.** A phone or a tablet stays online through ten minutes of silence. - **Mobile Safari fixes.** Pressing a control no longer sends the page to its top; tab tracks that do not fit scroll; switches animate after a press.
This commit is contained in:
1 parent
f8130e39a8
commit
ac90575b0b
788 files changed
+139244
-41720
No files matched your search
@@ -0,0 +1,251 @@
|
||||
// Package hub is the live state the backend shows its pages: named documents
|
||||
// (topics) and the ordered stream of their changes.
|
||||
//
|
||||
// A producer puts a whole document; the hub works out what changed and
|
||||
// publishes only that (a JSON Merge Patch), numbered. A page takes a snapshot
|
||||
// once, then follows the stream; one that fell behind (a tab that slept, a
|
||||
// connection that dropped) asks with the number it last saw and gets what it
|
||||
// missed, or a new snapshot when that is no longer kept. Nothing is published
|
||||
// for a document that did not change, so a producer may put as often as it
|
||||
// likes.
|
||||
//
|
||||
// The hub knows no transport: api serves it over ubus, portal over HTTP. It
|
||||
// owns no data of its own either: what a topic holds is its producer's.
|
||||
package hub
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// The operations of an event.
|
||||
const (
|
||||
OpPut = "put" // data is the whole document
|
||||
OpPatch = "patch" // data is a merge patch to the document
|
||||
OpAppend = "append" // data is { items, keep }: new entries of a list, and how many it holds at most
|
||||
OpDrop = "drop" // the topic is gone
|
||||
)
|
||||
|
||||
// Event is one change of one topic. Seq grows by one with every event of the
|
||||
// hub, whatever its topic: a reader that sees a number skipped has missed one.
|
||||
type Event struct {
|
||||
Seq uint64 `json:"seq"`
|
||||
Topic string `json:"topic"`
|
||||
Op string `json:"op"`
|
||||
Data any `json:"data,omitempty"`
|
||||
}
|
||||
|
||||
// Answer is what a reader gets when it asks where the hub is: the documents
|
||||
// (Docs, a snapshot) or the events since the number it gave (Events), and the
|
||||
// number it is at after applying them. Epoch names this run of the hub: a
|
||||
// number from another run means nothing.
|
||||
type Answer struct {
|
||||
Epoch string `json:"epoch"`
|
||||
Seq uint64 `json:"seq"`
|
||||
Docs map[string]any `json:"docs,omitempty"`
|
||||
Events []Event `json:"events,omitempty"`
|
||||
}
|
||||
|
||||
// journalSize is how many events are kept for readers that fell behind. A
|
||||
// reader further back gets a snapshot, which is always right, only larger.
|
||||
const journalSize = 512
|
||||
|
||||
// Hub holds the documents and publishes their changes.
|
||||
type Hub struct {
|
||||
mu sync.Mutex
|
||||
epoch string
|
||||
seq uint64
|
||||
docs map[string]any
|
||||
journal []Event
|
||||
wake chan struct{}
|
||||
subs map[*Sub]struct{}
|
||||
}
|
||||
|
||||
// New makes an empty hub with an epoch of its own.
|
||||
func New() *Hub {
|
||||
var b [6]byte
|
||||
rand.Read(b[:])
|
||||
return &Hub{
|
||||
epoch: hex.EncodeToString(b[:]),
|
||||
docs: map[string]any{},
|
||||
wake: make(chan struct{}),
|
||||
subs: map[*Sub]struct{}{},
|
||||
}
|
||||
}
|
||||
|
||||
// Epoch names this run of the hub.
|
||||
func (h *Hub) Epoch() string { return h.epoch }
|
||||
|
||||
// Put sets a topic's document and publishes what changed in it, if anything.
|
||||
// The value goes through encoding/json: a struct is published as its JSON.
|
||||
func (h *Hub) Put(topic string, doc any) error {
|
||||
next, err := plain(doc)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
h.mu.Lock()
|
||||
defer h.mu.Unlock()
|
||||
prev, had := h.docs[topic]
|
||||
if !had {
|
||||
h.docs[topic] = next
|
||||
h.publish(Event{Topic: topic, Op: OpPut, Data: next})
|
||||
return nil
|
||||
}
|
||||
patch, changed := Diff(prev, next)
|
||||
if !changed {
|
||||
return nil
|
||||
}
|
||||
h.docs[topic] = next
|
||||
// Only an object can be patched; anything else is sent whole.
|
||||
if _, object := next.(map[string]any); object {
|
||||
if _, was := prev.(map[string]any); was {
|
||||
h.publish(Event{Topic: topic, Op: OpPatch, Data: patch})
|
||||
return nil
|
||||
}
|
||||
}
|
||||
h.publish(Event{Topic: topic, Op: OpPut, Data: next})
|
||||
return nil
|
||||
}
|
||||
|
||||
// Append adds entries to a topic that is a list (a log, the points of a
|
||||
// meter) and publishes them; the list keeps its newest `keep` entries.
|
||||
func (h *Hub) Append(topic string, keep int, items ...any) error {
|
||||
if len(items) == 0 {
|
||||
return nil
|
||||
}
|
||||
added, err := plain(items)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
h.mu.Lock()
|
||||
defer h.mu.Unlock()
|
||||
old, _ := h.docs[topic].([]any)
|
||||
// A new slice every time: a snapshot handed out earlier is never written to.
|
||||
list := make([]any, 0, len(old)+len(items))
|
||||
list = append(append(list, old...), added.([]any)...)
|
||||
if keep > 0 && len(list) > keep {
|
||||
list = list[len(list)-keep:]
|
||||
}
|
||||
h.docs[topic] = list
|
||||
h.publish(Event{Topic: topic, Op: OpAppend, Data: map[string]any{"items": added, "keep": float64(keep)}})
|
||||
return nil
|
||||
}
|
||||
|
||||
// Drop removes a topic and tells its readers.
|
||||
func (h *Hub) Drop(topic string) {
|
||||
h.mu.Lock()
|
||||
defer h.mu.Unlock()
|
||||
if _, had := h.docs[topic]; !had {
|
||||
return
|
||||
}
|
||||
delete(h.docs, topic)
|
||||
h.publish(Event{Topic: topic, Op: OpDrop})
|
||||
}
|
||||
|
||||
// publish numbers an event, keeps it and hands it to whoever follows. Called
|
||||
// with the lock held.
|
||||
func (h *Hub) publish(e Event) {
|
||||
h.seq++
|
||||
e.Seq = h.seq
|
||||
h.journal = append(h.journal, e)
|
||||
if len(h.journal) > journalSize {
|
||||
h.journal = append([]Event(nil), h.journal[len(h.journal)-journalSize:]...)
|
||||
}
|
||||
close(h.wake)
|
||||
h.wake = make(chan struct{})
|
||||
for s := range h.subs {
|
||||
select {
|
||||
case s.c <- e:
|
||||
default:
|
||||
// A follower that does not keep up is let go; it asks again with Sync.
|
||||
delete(h.subs, s)
|
||||
close(s.c)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Sync answers a reader that is at (epoch, seq) and wants the topics `want`
|
||||
// accepts (nil: all): the events it missed when they are still kept, a
|
||||
// snapshot otherwise (another epoch, too far behind, or its first time: no epoch).
|
||||
func (h *Hub) Sync(epoch string, seq uint64, want func(topic string) bool) Answer {
|
||||
h.mu.Lock()
|
||||
defer h.mu.Unlock()
|
||||
return h.sync(epoch, seq, want)
|
||||
}
|
||||
|
||||
func (h *Hub) sync(epoch string, seq uint64, want func(string) bool) Answer {
|
||||
a := Answer{Epoch: h.epoch, Seq: h.seq}
|
||||
// A reader of this run that is not ahead of it, and whose next event is
|
||||
// still kept. Zero is a number like any other: a reader that took its
|
||||
// snapshot before anything was published follows from the first event.
|
||||
if epoch == h.epoch && seq <= h.seq && (seq == h.seq || (len(h.journal) > 0 && h.journal[0].Seq <= seq+1)) {
|
||||
a.Events = []Event{}
|
||||
for _, e := range h.journal {
|
||||
if e.Seq > seq && (want == nil || want(e.Topic)) {
|
||||
a.Events = append(a.Events, e)
|
||||
}
|
||||
}
|
||||
return a
|
||||
}
|
||||
a.Docs = make(map[string]any, len(h.docs))
|
||||
for topic, doc := range h.docs {
|
||||
if want == nil || want(topic) {
|
||||
a.Docs[topic] = doc
|
||||
}
|
||||
}
|
||||
return a
|
||||
}
|
||||
|
||||
// Wait is Sync that does not answer "nothing new": it waits until there is an
|
||||
// event the reader wants, or ctx ends (then the answer has no events, and the
|
||||
// reader asks again). A reader that needs a snapshot gets it at once.
|
||||
func (h *Hub) Wait(ctx context.Context, epoch string, seq uint64, want func(topic string) bool) Answer {
|
||||
for {
|
||||
h.mu.Lock()
|
||||
a := h.sync(epoch, seq, want)
|
||||
wake := h.wake
|
||||
h.mu.Unlock()
|
||||
if a.Docs != nil || len(a.Events) > 0 {
|
||||
return a
|
||||
}
|
||||
// Events of other topics moved the number: the reader is told, so its
|
||||
// next question is not answered from the start of the journal.
|
||||
seq = a.Seq
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return a
|
||||
case <-wake:
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Sub follows the hub: every event from the moment it subscribed, in order.
|
||||
type Sub struct {
|
||||
h *Hub
|
||||
c chan Event
|
||||
}
|
||||
|
||||
// Follow subscribes. The channel is closed when the follower did not keep up
|
||||
// (its buffer of 256 events filled) or called Close.
|
||||
func (h *Hub) Follow() *Sub {
|
||||
s := &Sub{h: h, c: make(chan Event, 256)}
|
||||
h.mu.Lock()
|
||||
h.subs[s] = struct{}{}
|
||||
h.mu.Unlock()
|
||||
return s
|
||||
}
|
||||
|
||||
// Events is the follower's channel.
|
||||
func (s *Sub) Events() <-chan Event { return s.c }
|
||||
|
||||
// Close ends the subscription.
|
||||
func (s *Sub) Close() {
|
||||
s.h.mu.Lock()
|
||||
if _, on := s.h.subs[s]; on {
|
||||
delete(s.h.subs, s)
|
||||
close(s.c)
|
||||
}
|
||||
s.h.mu.Unlock()
|
||||
}
|
||||
@@ -0,0 +1,177 @@
|
||||
package hub
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func doc(s string) any {
|
||||
var v any
|
||||
if err := json.Unmarshal([]byte(s), &v); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
// A patch holds only what changed, and applying it gives the new document.
|
||||
func TestDiffAndMerge(t *testing.T) {
|
||||
cases := []struct{ a, b, patch string }{
|
||||
{`{"a":1,"b":2}`, `{"a":1,"b":3}`, `{"b":3}`},
|
||||
{`{"a":1,"b":2}`, `{"a":1}`, `{"b":null}`},
|
||||
{`{"a":1}`, `{"a":1,"c":{"d":true}}`, `{"c":{"d":true}}`},
|
||||
{`{"p":{"d1":{"online":false,"rx":1},"d2":{"online":true}}}`, `{"p":{"d1":{"online":true,"rx":1},"d2":{"online":true}}}`, `{"p":{"d1":{"online":true}}}`},
|
||||
{`{"l":[1,2,3]}`, `{"l":[1,2]}`, `{"l":[1,2]}`},
|
||||
{`{"a":{"b":1}}`, `{"a":"text"}`, `{"a":"text"}`},
|
||||
{`{"a":"text"}`, `{"a":{"b":1}}`, `{"a":{"b":1}}`},
|
||||
}
|
||||
for _, c := range cases {
|
||||
patch, changed := Diff(doc(c.a), doc(c.b))
|
||||
if !changed {
|
||||
t.Fatalf("%s → %s: no change seen", c.a, c.b)
|
||||
}
|
||||
if !reflect.DeepEqual(patch, doc(c.patch)) {
|
||||
got, _ := json.Marshal(patch)
|
||||
t.Fatalf("%s → %s: patch %s, want %s", c.a, c.b, got, c.patch)
|
||||
}
|
||||
if got := Merge(doc(c.a), patch); !reflect.DeepEqual(got, doc(c.b)) {
|
||||
t.Fatalf("%s + %s did not give %s", c.a, c.patch, c.b)
|
||||
}
|
||||
}
|
||||
if _, changed := Diff(doc(`{"a":[1,{"b":2}]}`), doc(`{"a":[1,{"b":2}]}`)); changed {
|
||||
t.Fatal("equal documents differ")
|
||||
}
|
||||
}
|
||||
|
||||
// Nulls never reach a document: a patch could not tell one from a removal.
|
||||
func TestPlainDropsNulls(t *testing.T) {
|
||||
got, err := plain(map[string]any{"a": nil, "b": map[string]any{"c": nil, "d": 1}, "l": []any{nil, 1}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !reflect.DeepEqual(got, doc(`{"b":{"d":1},"l":[null,1]}`)) {
|
||||
t.Fatalf("got %v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPutPublishesOnlyChanges(t *testing.T) {
|
||||
h := New()
|
||||
sub := h.Follow()
|
||||
defer sub.Close()
|
||||
h.Put("people", doc(`{"p1":{"name":"Myata","online":false}}`))
|
||||
h.Put("people", doc(`{"p1":{"name":"Myata","online":false}}`)) // the same: nothing
|
||||
h.Put("people", doc(`{"p1":{"name":"Myata","online":true}}`))
|
||||
first, second := <-sub.Events(), <-sub.Events()
|
||||
if first.Seq != 1 || first.Op != OpPut || second.Seq != 2 || second.Op != OpPatch {
|
||||
t.Fatalf("events: %+v %+v", first, second)
|
||||
}
|
||||
if !reflect.DeepEqual(second.Data, doc(`{"p1":{"online":true}}`)) {
|
||||
t.Fatalf("patch: %v", second.Data)
|
||||
}
|
||||
select {
|
||||
case e := <-sub.Events():
|
||||
t.Fatalf("an event for nothing: %+v", e)
|
||||
default:
|
||||
}
|
||||
}
|
||||
|
||||
// A reader gets a snapshot first, then what it missed; from another run or
|
||||
// too far behind, a snapshot again.
|
||||
func TestSync(t *testing.T) {
|
||||
h := New()
|
||||
h.Put("a", doc(`{"x":1}`))
|
||||
h.Put("b", doc(`{"y":1}`))
|
||||
snap := h.Sync("", 0, nil)
|
||||
if snap.Seq != 2 || len(snap.Docs) != 2 || snap.Events != nil {
|
||||
t.Fatalf("snapshot: %+v", snap)
|
||||
}
|
||||
h.Put("a", doc(`{"x":2}`))
|
||||
h.Put("b", doc(`{"y":2}`))
|
||||
only := func(topic string) bool { return topic == "a" }
|
||||
got := h.Sync(snap.Epoch, snap.Seq, only)
|
||||
if got.Docs != nil || len(got.Events) != 1 || got.Events[0].Topic != "a" || got.Seq != 4 {
|
||||
t.Fatalf("catch-up: %+v", got)
|
||||
}
|
||||
// A snapshot taken before anything was published is followed from the first event.
|
||||
if early := h.Sync(snap.Epoch, 0, nil); early.Docs != nil || len(early.Events) != 4 {
|
||||
t.Fatalf("from zero: %+v", early)
|
||||
}
|
||||
if up := h.Sync(snap.Epoch, 4, nil); up.Docs != nil || len(up.Events) != 0 {
|
||||
t.Fatalf("up to date: %+v", up)
|
||||
}
|
||||
if other := h.Sync("another-run", 3, nil); other.Docs == nil {
|
||||
t.Fatal("a number of another run was taken for ours")
|
||||
}
|
||||
if ahead := h.Sync(snap.Epoch, 99, nil); ahead.Docs == nil {
|
||||
t.Fatal("a number ahead of the hub was accepted")
|
||||
}
|
||||
for i := 0; i < journalSize+10; i++ {
|
||||
h.Put("a", map[string]any{"x": i + 10})
|
||||
}
|
||||
if far := h.Sync(snap.Epoch, 4, nil); far.Docs == nil {
|
||||
t.Fatal("events no longer kept were promised")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAppendKeepsNewest(t *testing.T) {
|
||||
h := New()
|
||||
h.Append("meter", 3, 1, 2)
|
||||
h.Append("meter", 3, 3, 4)
|
||||
snap := h.Sync("", 0, nil)
|
||||
if !reflect.DeepEqual(snap.Docs["meter"], doc(`[2,3,4]`)) {
|
||||
t.Fatalf("list: %v", snap.Docs["meter"])
|
||||
}
|
||||
ev := h.Sync(snap.Epoch, 1, nil).Events
|
||||
if len(ev) != 1 || ev[0].Op != OpAppend || !reflect.DeepEqual(ev[0].Data, doc(`{"items":[3,4],"keep":3}`)) {
|
||||
t.Fatalf("event: %+v", ev)
|
||||
}
|
||||
}
|
||||
|
||||
// Wait holds until something the reader wants happens, and gives up with its context.
|
||||
func TestWait(t *testing.T) {
|
||||
h := New()
|
||||
h.Put("a", doc(`{"x":1}`))
|
||||
at := h.Sync("", 0, nil)
|
||||
only := func(topic string) bool { return topic == "a" }
|
||||
got := make(chan Answer, 1)
|
||||
go func() { got <- h.Wait(context.Background(), at.Epoch, at.Seq, only) }()
|
||||
h.Put("b", doc(`{"y":1}`)) // not wanted: the wait goes on
|
||||
select {
|
||||
case a := <-got:
|
||||
t.Fatalf("answered for another topic: %+v", a)
|
||||
case <-time.After(50 * time.Millisecond):
|
||||
}
|
||||
h.Put("a", doc(`{"x":2}`))
|
||||
select {
|
||||
case a := <-got:
|
||||
if len(a.Events) != 1 || a.Seq != 3 {
|
||||
t.Fatalf("answer: %+v", a)
|
||||
}
|
||||
case <-time.After(time.Second):
|
||||
t.Fatal("never answered")
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Millisecond)
|
||||
defer cancel()
|
||||
if a := h.Wait(ctx, at.Epoch, 3, only); len(a.Events) != 0 || a.Docs != nil || a.Seq != 3 {
|
||||
t.Fatalf("timed out with: %+v", a)
|
||||
}
|
||||
}
|
||||
|
||||
// A follower that does not read is let go, not waited for.
|
||||
func TestSlowFollowerIsDropped(t *testing.T) {
|
||||
h := New()
|
||||
sub := h.Follow()
|
||||
for i := 0; i < 300; i++ {
|
||||
h.Put("a", map[string]any{"x": i})
|
||||
}
|
||||
n := 0
|
||||
for range sub.Events() {
|
||||
n++
|
||||
}
|
||||
if n != 256 {
|
||||
t.Fatalf("read %d events before the channel closed", n)
|
||||
}
|
||||
sub.Close() // closing again is harmless
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
// The difference between two versions of a document as a JSON Merge Patch
|
||||
// (RFC 7396): an object that holds only what changed, null where a field went.
|
||||
// Documents are what encoding/json makes of JSON (map[string]any, []any,
|
||||
// float64, string, bool); they hold no null, which a merge patch could not
|
||||
// tell from "remove this" (plain() drops them on the way in).
|
||||
//
|
||||
// Pure: no state, no clock. hub.go keeps the versions.
|
||||
package hub
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
// plain turns any value into a document: through encoding/json, so a struct
|
||||
// is seen the way a client will see it, and without nulls.
|
||||
func plain(v any) (any, error) {
|
||||
raw, err := json.Marshal(v)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var out any
|
||||
if err := json.Unmarshal(raw, &out); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return dropNulls(out), nil
|
||||
}
|
||||
|
||||
// dropNulls removes null fields of objects, at any depth. A null inside an
|
||||
// array stays: an array is replaced whole by a patch, never merged.
|
||||
func dropNulls(v any) any {
|
||||
switch x := v.(type) {
|
||||
case map[string]any:
|
||||
for k, e := range x {
|
||||
if e == nil {
|
||||
delete(x, k)
|
||||
} else {
|
||||
x[k] = dropNulls(e)
|
||||
}
|
||||
}
|
||||
case []any:
|
||||
for i, e := range x {
|
||||
x[i] = dropNulls(e)
|
||||
}
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
// Diff returns the merge patch that turns document a into b, and whether they
|
||||
// differ at all. Objects are compared field by field; anything else (an
|
||||
// array, a number, a string) is replaced whole when it changed. The patch
|
||||
// shares b's values: neither may be changed afterwards.
|
||||
func Diff(a, b any) (any, bool) {
|
||||
am, aok := a.(map[string]any)
|
||||
bm, bok := b.(map[string]any)
|
||||
if !aok || !bok {
|
||||
if reflect.DeepEqual(a, b) {
|
||||
return nil, false
|
||||
}
|
||||
return b, true
|
||||
}
|
||||
patch := map[string]any{}
|
||||
for k := range am {
|
||||
if _, still := bm[k]; !still {
|
||||
patch[k] = nil
|
||||
}
|
||||
}
|
||||
for k, bv := range bm {
|
||||
av, had := am[k]
|
||||
if !had {
|
||||
patch[k] = bv
|
||||
continue
|
||||
}
|
||||
if p, changed := Diff(av, bv); changed {
|
||||
patch[k] = p
|
||||
}
|
||||
}
|
||||
return patch, len(patch) > 0
|
||||
}
|
||||
|
||||
// Merge applies a merge patch to a document and returns the result. The
|
||||
// document is changed in place where it is an object; what a client does with
|
||||
// a patch, kept here so a test can hold Diff to it.
|
||||
func Merge(doc, patch any) any {
|
||||
pm, ok := patch.(map[string]any)
|
||||
if !ok {
|
||||
return patch
|
||||
}
|
||||
dm, ok := doc.(map[string]any)
|
||||
if !ok {
|
||||
dm = map[string]any{}
|
||||
}
|
||||
for k, pv := range pm {
|
||||
if pv == nil {
|
||||
delete(dm, k)
|
||||
continue
|
||||
}
|
||||
dm[k] = Merge(dm[k], pv)
|
||||
}
|
||||
return dm
|
||||
}
|
||||
Reference in new issue
Block a user