From b397c42b3cabc4202e667556e1f968d8dede3767 Mon Sep 17 00:00:00 2001 From: Martin Martinez Rivera Date: Tue, 23 Jul 2019 15:05:39 -0700 Subject: [PATCH] Fix error when exporting a predicate to the schema. (#3700) The current logic only adds the '<' and '>' characters around the predicate list if it has something other than numbers and letters. However, this logic is faulty since a predicate consisting of only numbers needs to have the surrounding brackets to be properly parsed. This change removes that logic and adds the surrounding brackets to all predicates for simplicity and correctness. --- worker/export.go | 28 +++------------------------- worker/export_test.go | 6 +++--- 2 files changed, 6 insertions(+), 28 deletions(-) diff --git a/worker/export.go b/worker/export.go index 9aa560b22dd..edfabc21163 100644 --- a/worker/export.go +++ b/worker/export.go @@ -27,7 +27,6 @@ import ( "path/filepath" "strings" "time" - "unicode" "github.com/golang/glog" "github.com/pkg/errors" @@ -88,20 +87,6 @@ var rdfTypeMap = map[types.TypeID]string{ types.PasswordID: "xs:password", } -// Having '<' and '>' around all predicates makes the exported schema harder -// for humans to look at, so only put them on predicates containing "exotic" -// characters (i.e. ones not in this list). -var predNonSpecialChars = unicode.RangeTable{ - R16: []unicode.Range16{ - // Ranges must be in order. - {'.', '.', 1}, - {'0', '9', 1}, - {'A', 'Z', 1}, - {'_', '_', 1}, - {'a', 'z', 1}, - }, -} - // UIDs like 0x1 look weird but 64-bit ones like 0x0000000000000001 are too long. var uidFmtStrRdf = "<0x%x>" var uidFmtStrJson = "\"0x%x\"" @@ -293,16 +278,9 @@ func (e *exporter) toRDF() (*bpb.KVList, error) { func toSchema(attr string, update pb.SchemaUpdate) (*bpb.KVList, error) { // bytes.Buffer never returns error for any of the writes. So, we don't need to check them. var buf bytes.Buffer - isSpecial := func(r rune) bool { - return !(unicode.In(r, &predNonSpecialChars)) - } - if strings.IndexFunc(attr, isSpecial) >= 0 { - buf.WriteRune('<') - buf.WriteString(attr) - buf.WriteRune('>') - } else { - buf.WriteString(attr) - } + buf.WriteRune('<') + buf.WriteString(attr) + buf.WriteRune('>') buf.WriteByte(':') if update.List { buf.WriteRune('[') diff --git a/worker/export_test.go b/worker/export_test.go index 848ba70ba6d..c9d0cda0a0f 100644 --- a/worker/export_test.go +++ b/worker/export_test.go @@ -370,7 +370,7 @@ func TestToSchema(t *testing.T) { Lang: true, }, }, - expected: "Alice:string @reverse @count @lang @upsert . \n", + expected: ":string @reverse @count @lang @upsert . \n", }, { skv: &skv{ @@ -445,7 +445,7 @@ func TestToSchema(t *testing.T) { Lang: true, }, }, - expected: "data_base:string @lang . \n", + expected: ":string @lang . \n", }, { skv: &skv{ @@ -460,7 +460,7 @@ func TestToSchema(t *testing.T) { Lang: true, }, }, - expected: "data.base:string @lang . \n", + expected: ":string @lang . \n", }, } for _, testCase := range testCases {