Changeset 0.15.0 (#71)
This commit is contained in:
38
server/treepath/summary.go
Normal file
38
server/treepath/summary.go
Normal file
@@ -0,0 +1,38 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user