Skip to content

Commit

Permalink
fix(Dgraph): Replace fmt.Sprintf with strconv.FormatUint int outputrd…
Browse files Browse the repository at this point in the history
…f.go (#6310)
  • Loading branch information
martinmr authored Sep 1, 2020
1 parent bf79999 commit 4b375b3
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions query/outputrdf.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func (b *rdfBuilder) rdfForSubgraph(sg *SubGraph) error {

func (b *rdfBuilder) writeRDF(subject uint64, predicate []byte, object []byte) {
// add subject
b.writeTriple([]byte(fmt.Sprintf("%#x", subject)))
x.Check2(b.buf.Write(toHex(subject)))
x.Check(b.buf.WriteByte(' '))
// add predicate
b.writeTriple(predicate)
Expand Down Expand Up @@ -149,19 +149,14 @@ func (b *rdfBuilder) rdfForUIDList(subject uint64, list *pb.List, sg *SubGraph)
continue
}
// Build object.
b.writeRDF(
subject,
[]byte(sg.fieldName()),
buildTriple([]byte(fmt.Sprintf("%#x", destUID))))
b.writeRDF(subject, []byte(sg.fieldName()), toHex(destUID))
}
}

// rdfForValueList returns rdf for the value list.
func (b *rdfBuilder) rdfForValueList(subject uint64, valueList *pb.ValueList, attr string) {
if attr == "uid" {
b.writeRDF(subject,
[]byte(attr),
buildTriple([]byte(fmt.Sprintf("%#x", subject))))
b.writeRDF(subject, []byte(attr), toHex(subject))
return
}
for _, destValue := range valueList.Values {
Expand Down Expand Up @@ -234,3 +229,16 @@ func quotedNumber(val []byte) []byte {
tmpVal = append(tmpVal, '"')
return tmpVal
}

func toHex(uid uint64) []byte {
var buf [16]byte
tmp := strconv.AppendUint(buf[:0], uid, 16)

out := make([]byte, len(tmp)+3+1)
out[0] = '<'
out[1] = '0'
out[2] = 'x'
n := copy(out[3:], tmp)
out[3+n] = '>'
return out
}

0 comments on commit 4b375b3

Please sign in to comment.