Files
dibs/internal/config/positions.go
T
Overlord 2175c0e5aa Add scheduler, rate limiting and headless mode.
Hard-coded 60s URL and 10s host floors with jitter and per-host
backoff, shared between dibs processes through a locked state file.
The scheduler shuffles due URLs each round and runs one worker per
host; 'dibs --headless' watches and logs JSON.
2026-10-14 09:04:15 +02:00

133 lines
3.0 KiB
Go

package config
import (
"fmt"
"strings"
"github.com/pelletier/go-toml/v2/unstable"
)
// positions maps key paths such as "item[1].interval" or
// "notify.webhooks[0].url" to where they appear in the document, so that
// validation errors can point at a line.
type positions map[string]unstable.Position
func indexPositions(doc []byte) positions {
ps := positions{}
var p unstable.Parser
p.Reset(doc)
prefix := ""
arrays := map[string]int{}
// A parse error stops the index early. The decoder reports it.
for p.NextExpression() {
e := p.Expression()
if e == nil { continue }
switch e.Kind {
case unstable.Table, unstable.ArrayTable:
keys, first := keyParts(e.Key())
path := strings.Join(keys, ".")
if e.Kind == unstable.ArrayTable {
n := arrays[path]
arrays[path]++
path = fmt.Sprintf("%s[%d]", path, n)
}
prefix = path
ps.add(&p, path, first)
case unstable.KeyValue:
ps.keyValue(&p, prefix, e)
default:
// Comments and other expressions have no key to record.
}
}
return ps
}
func (ps positions) keyValue(p *unstable.Parser, prefix string, kv *unstable.Node) {
keys, first := keyParts(kv.Key())
path := strings.Join(keys, ".")
if prefix != "" {
path = prefix + "." + path
}
ps.add(p, path, first)
ps.value(p, path, kv.Value())
}
func (ps positions) value(p *unstable.Parser, path string, v *unstable.Node) {
switch v.Kind {
case unstable.Array:
it := v.Children()
for i := 0; it.Next(); i++ {
el := it.Node()
elPath := fmt.Sprintf("%s[%d]", path, i)
ps.add(p, elPath, el)
ps.value(p, elPath, el)
}
case unstable.InlineTable:
it := v.Children()
for it.Next() {
if kv := it.Node(); kv.Kind == unstable.KeyValue { ps.keyValue(p, path, kv) }
}
default:
// Scalars have no nested keys.
}
}
func (ps positions) add(p *unstable.Parser, path string, n *unstable.Node) {
if _, ok := ps[path]; !ok && n != nil {
ps[path] = p.Shape(n.Raw).Start
}
}
// find returns the position of path, or of its closest present parent, so
// that a missing key points at the table it belongs in.
func (ps positions) find(path string) (unstable.Position, bool) {
for path != "" {
if pos, ok := ps[path]; ok { return pos, true }
i := strings.LastIndexAny(path, ".[")
if i < 0 {
break
}
path = path[:i]
}
return unstable.Position{}, false
}
// keyAt returns the path of the last key that starts on line at or before
// col, or "" if there is none.
func (ps positions) keyAt(line, col int) string {
best, bestCol := "", 0
for path, p := range ps {
if p.Line == line && p.Column <= col && p.Column >= bestCol {
// An array element and its first key can share a position; the
// longer path wins.
if p.Column > bestCol || len(path) > len(best) {
best, bestCol = path, p.Column
}
}
}
return best
}
func keyParts(it unstable.Iterator) ([]string, *unstable.Node) {
var keys []string
var first *unstable.Node
for it.Next() {
n := it.Node()
if first == nil { first = n }
keys = append(keys, string(n.Data))
}
return keys, first
}