extractschema: Include common node facts in the result.
Summary:
Add a new section to the result message that includes the common node facts
shared by various nodes in the schema, regardless of kind.
Reviewers: danielmoy-google, #core_team
Subscribers: #core_team
@@ -38,6 +38,7 @@ import (
// Schema represents the schema as a whole.
type Schema struct {
+ Common []*Fact `json:"common,omitempty"`
Nodes []*Node `json:"nodes,omitempty"`
Edges []*Edge `json:"edges,omitempty"`
VName *Name `json:"vname,omitempty"`
@@ -114,8 +115,15 @@ type Fact struct {
Label string `json:"label"`
Description string `json:"description,omitempty"`
Values []string `json:"values,omitempty"`
+ AttachTo string `json:"attachTo,omitempty"`
}
+type factsByLabel []*Fact
+
+func (b factsByLabel) Len() int { return len(b) }
+func (b factsByLabel) Less(i, j int) bool { return b[i].Label < b[j].Label }
+func (b factsByLabel) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
+
var (
schemaFile = flag.String("schema", "", "Schema file path (required)")
@@ -155,6 +163,11 @@ func main() {
schema.VName = extractNameRules(s)
}
+ if s, ok := sections["common node facts"]; ok {
+ schema.Common = extractFacts(s)
+ }
+ sort.Sort(factsByLabel(schema.Common))
+
// Add the kind of each edge to the edges set of any node mentioned in the
// source or targets list for that edge.
for _, edge := range schema.Edges {
@@ -241,6 +254,28 @@ func extractNodeKinds(s string) []*Node {
return out
}
+func extractFacts(s string) []*Fact {
+ var out []*Fact
+
+ for label, text := range splitOnRegexp(kindHeader, s) {
+ labels := splitOnRegexp(mainLabel, text)
+ fact := &Fact{
+ Label: label,
+ Description: cleanText(labels["brief description"]),
+ }
+ switch t := cleanText(labels["attached to"]); t {
+ case "all nodes":
+ fact.AttachTo = "all"
+ case "semantic nodes":
+ fact.AttachTo = "semantic"
+ default:
+ log.Printf("WARNING: Unknown attachment kind: %q", t)
+ }
+ out = append(out, fact)
+ }
+ return out
+}
+
func relatedKinds(s string) []string {
var rel []string
for _, target := range kindLink.FindAllStringSubmatch(s, -1) {
