Beispiel #1
0
// RawAnchor returns and coerces n if it is an anchor node.  nil will be returned
// otherwise.
func RawAnchor(n *ipb.Path_Node) *srvpb.RawAnchor {
	if a := n.GetRawAnchor(); a != nil {
		return a
	} else if a := n.GetExpandedAnchor(); a != nil {
		return &srvpb.RawAnchor{
			Ticket:       a.Ticket,
			StartOffset:  a.Span.Start.ByteOffset,
			EndOffset:    a.Span.End.ByteOffset,
			SnippetStart: a.SnippetSpan.Start.ByteOffset,
			SnippetEnd:   a.SnippetSpan.End.ByteOffset,
		}
	}
	return nil
}
Beispiel #2
0
// OriginalNode returns the given node's original serving node, synthesizing one
// if necessary.
func OriginalNode(n *ipb.Path_Node) (g *srvpb.Node) {
	defer func() {
		// Ensure serving data is consistent
		sort.Sort(xrefs.ByName(g.Fact))
	}()

	if n.Original != nil {
		return n.Original
	}

	// Synthesize node without original
	log.Printf("WARNING: synthesizing node for %q", n.Ticket)
	sn := &srvpb.Node{
		Ticket: n.Ticket,
		Fact: []*cpb.Fact{
			{Name: facts.NodeKind, Value: []byte(n.NodeKind)},
		},
	}

	if a := n.GetRawAnchor(); a != nil {
		sn.Fact = append(sn.Fact,
			&cpb.Fact{Name: facts.AnchorStart, Value: []byte(strconv.FormatInt(int64(a.StartOffset), 10))},
			&cpb.Fact{Name: facts.AnchorEnd, Value: []byte(strconv.FormatInt(int64(a.EndOffset), 10))},
		)
		if a.SnippetStart != 0 || a.SnippetEnd != 0 {
			sn.Fact = append(sn.Fact,
				&cpb.Fact{Name: facts.SnippetStart, Value: []byte(strconv.FormatInt(int64(a.SnippetStart), 10))},
				&cpb.Fact{Name: facts.SnippetEnd, Value: []byte(strconv.FormatInt(int64(a.SnippetEnd), 10))},
			)
		}
	} else if a := n.GetExpandedAnchor(); a != nil {
		sn.Fact = append(sn.Fact,
			&cpb.Fact{Name: facts.AnchorStart, Value: []byte(strconv.FormatInt(int64(a.Span.Start.ByteOffset), 10))},
			&cpb.Fact{Name: facts.AnchorEnd, Value: []byte(strconv.FormatInt(int64(a.Span.End.ByteOffset), 10))},
		)
		if a.SnippetSpan.Start.ByteOffset != 0 || a.SnippetSpan.End.ByteOffset != 0 {
			sn.Fact = append(sn.Fact,
				&cpb.Fact{Name: facts.SnippetStart, Value: []byte(strconv.FormatInt(int64(a.SnippetSpan.Start.ByteOffset), 10))},
				&cpb.Fact{Name: facts.SnippetEnd, Value: []byte(strconv.FormatInt(int64(a.SnippetSpan.End.ByteOffset), 10))},
			)
		}
	} else if f := n.GetFile(); f != nil {
		if len(f.Text) > 0 {
			sn.Fact = append(sn.Fact, &cpb.Fact{Name: facts.Text, Value: f.Text})
		}
		if f.Encoding != "" {
			sn.Fact = append(sn.Fact, &cpb.Fact{Name: facts.TextEncoding, Value: []byte(f.Encoding)})
		}
	}

	return sn
}