39 lines
926 B
Go
39 lines
926 B
Go
package treepath
|
|
|
|
import "encoding/json"
|
|
|
|
// IsSummaryMessage checks if a PathMessage has summary metadata
|
|
// (metadata.type == "summary").
|
|
func IsSummaryMessage(m *PathMessage) bool {
|
|
if m.Metadata == nil {
|
|
return false
|
|
}
|
|
meta := ParseMetadata(m)
|
|
return meta["type"] == "summary"
|
|
}
|
|
|
|
// ParseMetadata unmarshals the JSONB metadata on a PathMessage.
|
|
// Returns an empty map on any error.
|
|
func ParseMetadata(m *PathMessage) map[string]interface{} {
|
|
if m.Metadata == nil {
|
|
return map[string]interface{}{}
|
|
}
|
|
var meta map[string]interface{}
|
|
if err := json.Unmarshal(*m.Metadata, &meta); err != nil {
|
|
return map[string]interface{}{}
|
|
}
|
|
return meta
|
|
}
|
|
|
|
// FindSummaryBoundary returns the index of the most recent summary node
|
|
// in a path, or -1 if no summary exists.
|
|
func FindSummaryBoundary(path []PathMessage) int {
|
|
idx := -1
|
|
for i := range path {
|
|
if IsSummaryMessage(&path[i]) {
|
|
idx = i
|
|
}
|
|
}
|
|
return idx
|
|
}
|