Skip to content

Commit

Permalink
sort kvs and make the config compositing work
Browse files Browse the repository at this point in the history
  • Loading branch information
mleku committed Dec 5, 2024
1 parent 9f38e1c commit fe63247
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
14 changes: 11 additions & 3 deletions realy/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"path/filepath"
"reflect"
"sort"
"strings"

"go-simpler.org/env"
Expand Down Expand Up @@ -91,6 +92,10 @@ type KV struct{ Key, Value st }

type KVSlice []KV

func (kv KVSlice) Len() int { return len(kv) }
func (kv KVSlice) Less(i, j int) bool { return kv[i].Key < kv[j].Key }
func (kv KVSlice) Swap(i, j int) { kv[i], kv[j] = kv[j], kv[i] }

// Composit merges two KVSlice together, replacing the values of earlier keys with same named
// KV items later in the slice (enabling compositing two together as a .env, as well as them
// being composed as structs.
Expand All @@ -99,15 +104,16 @@ func (kv KVSlice) Composit(kv2 KVSlice) (out KVSlice) {
for _, p := range kv {
out = append(out, p)
}
out:
for i, p := range kv2 {
out:
for j, q := range out {
// if the key is repeated, replace the value
if p.Key == q.Key {
out[j].Value = kv2[i].Value
break out
continue out
}
}
out = append(out, p)
}
return
}
Expand Down Expand Up @@ -144,7 +150,9 @@ func EnvKV(cfg any) (m KVSlice) {
}

func PrintEnv(cfg *C, printer io.Writer) {
for _, v := range EnvKV(*cfg) {
kvs := EnvKV(*cfg)
sort.Sort(kvs)
for _, v := range kvs {
_, _ = fmt.Fprintf(printer, "%s=%s\n", v.Key, v.Value)
}
}
Expand Down
2 changes: 1 addition & 1 deletion realy/version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v1.2.35
v1.2.36

0 comments on commit fe63247

Please sign in to comment.