kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/go/util/schema/schema.go (about)

     1  /*
     2   * Copyright 2014 The Kythe Authors. All rights reserved.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *   http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  // Package schema defines constants used in the Kythe schema.
    18  package schema // import "kythe.io/kythe/go/util/schema"
    19  
    20  import (
    21  	"kythe.io/kythe/go/util/schema/facts"
    22  
    23  	scpb "kythe.io/kythe/proto/schema_go_proto"
    24  	spb "kythe.io/kythe/proto/storage_go_proto"
    25  )
    26  
    27  // Prefix is the label prefix for the Kythe schema.
    28  const Prefix = "/kythe/"
    29  
    30  const (
    31  	// AnchorLocFilter is a fact filter for anchor locations.
    32  	AnchorLocFilter = "/kythe/loc/*"
    33  
    34  	// SnippetLocFilter is a fact filter for snippet locations.
    35  	SnippetLocFilter = "/kythe/snippet/*"
    36  )
    37  
    38  // An Edge represents an edge.
    39  type Edge struct {
    40  	Source, Target *spb.VName
    41  	Kind           string
    42  }
    43  
    44  // ToEntry converts e to a kythe.proto.Entry message.
    45  func (e *Edge) ToEntry() *spb.Entry {
    46  	return &spb.Entry{
    47  		Source:   e.Source,
    48  		Target:   e.Target,
    49  		EdgeKind: e.Kind,
    50  		FactName: "/",
    51  	}
    52  }
    53  
    54  // Facts represents a collection of key/value facts.
    55  type Facts map[string]string
    56  
    57  // A Node represents a collection of facts about a node.
    58  type Node struct {
    59  	VName *spb.VName
    60  	Kind  string
    61  	Facts Facts
    62  }
    63  
    64  // AddFact adds the specified fact to n, replacing any previous value for that
    65  // fact that may exist.
    66  func (n *Node) AddFact(name, value string) {
    67  	if n.Facts == nil {
    68  		n.Facts = make(Facts)
    69  	}
    70  	n.Facts[name] = value
    71  }
    72  
    73  // ToEntries converts n to a slice of kythe.proto.Entry messages. The result
    74  // will have at least one entry for the node kind. If n contains a text fact
    75  // and does not supply an explicit encoding, the default one is also added.
    76  // The resulting slice is not ordered.
    77  func (n *Node) ToEntries() []*spb.Entry {
    78  	var entries []*spb.Entry
    79  	add := func(key, value string) {
    80  		entries = append(entries, &spb.Entry{
    81  			Source:    n.VName,
    82  			FactName:  key,
    83  			FactValue: []byte(value),
    84  		})
    85  	}
    86  
    87  	add(facts.NodeKind, n.Kind) // ensure the node kind exists.
    88  	for name, value := range n.Facts {
    89  		add(name, value)
    90  	}
    91  
    92  	// If a text fact was emitted, ensure there is an encoding too.
    93  	if _, hasText := n.Facts[facts.Text]; hasText {
    94  		if _, hasEncoding := n.Facts[facts.TextEncoding]; !hasEncoding {
    95  			add(facts.TextEncoding, facts.DefaultTextEncoding)
    96  		}
    97  	}
    98  	return entries
    99  }
   100  
   101  // GetNodeKind returns the string representation of the node's kind.
   102  func GetNodeKind(n *scpb.Node) string {
   103  	if k := n.GetGenericKind(); k != "" {
   104  		return k
   105  	}
   106  	return NodeKindString(n.GetKytheKind())
   107  }
   108  
   109  // GetSubkind returns the string representation of the node's subkind.
   110  func GetSubkind(n *scpb.Node) string {
   111  	if k := n.GetGenericSubkind(); k != "" {
   112  		return k
   113  	}
   114  	return SubkindString(n.GetKytheSubkind())
   115  }
   116  
   117  // GetFactName returns the string representation of the fact's name.
   118  func GetFactName(f *scpb.Fact) string {
   119  	if k := f.GetGenericName(); k != "" {
   120  		return k
   121  	}
   122  	return FactNameString(f.GetKytheName())
   123  }
   124  
   125  // GetEdgeKind returns the string representation of the edge's kind.
   126  func GetEdgeKind(e *scpb.Edge) string {
   127  	if k := e.GetGenericKind(); k != "" {
   128  		return k
   129  	}
   130  	return EdgeKindString(e.GetKytheKind())
   131  }