Files
mesh-sdk/go/views/ownbuild.go
T

45 lines
1.5 KiB
Go

package views
import (
"context"
"encoding/json"
"os"
"git.novox.be/novox/mesh-sdk/go/process"
)
// OwnBuildKey is where a process keeps its own build in its state `first-views`, and the field both answers of a part's
// first views carry beside their list (design 47 G §674, §424, §519): the fingerprint the node-engine composed into
// the part's environment, or OwnBuildUnknown when it carries none.
const OwnBuildKey = "own-build"
// OwnBuildUnknown is what a part's first views say of its own build when its environment carries no fingerprint.
const OwnBuildUnknown = "unknown"
func ownBuildWord(b process.Build) string {
if b.Fingerprint == "" {
return OwnBuildUnknown
}
return b.Fingerprint
}
// ownBuild is the build this reader's bundle was composed with, read from its environment at each answer.
func (r *Reader) ownBuild() process.Build {
getenv := r.Getenv
if getenv == nil {
getenv = os.Getenv
}
return process.ReadOwnBuild(getenv)
}
// KeepOwnBuild writes a process's own build at OwnBuildKey of kv (its state `first-views`) at its start, for its tools
// bundle's status tool to read (design 47 G §674).
func KeepOwnBuild(ctx context.Context, kv interface {
Put(ctx context.Context, key string, value []byte) (uint64, error)
}, b process.Build, logf func(string, ...any)) {
data, _ := json.Marshal(ownBuildWord(b))
if _, err := kv.Put(ctx, OwnBuildKey, data); err != nil && logf != nil {
logf("views: the own build is not kept in %s: %v", StatusState, err)
}
}