From e9135c9c10bddeac6e1f66af5179241336c0fc97 Mon Sep 17 00:00:00 2001 From: Gibheer Date: Fri, 7 May 2021 21:17:13 +0200 Subject: [PATCH] add missing table name The table name was missing from the implementation of FieldsToJSON. This was also fixed in the tests. --- query/query.go | 4 ++-- query/query_test.go | 13 ++++++++++++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/query/query.go b/query/query.go index cf567c0..fd87f86 100644 --- a/query/query.go +++ b/query/query.go @@ -43,9 +43,9 @@ func FieldListToSelect(tableName string, fl types.FieldList, nameMap map[string] func FieldsToJSON(table string, fields map[string]string) string { res := []string{} for key, val := range fields { - res = append(res, fmt.Sprintf("'%s', %s", key, val)) + res = append(res, fmt.Sprintf("'%s',%s", key, val)) } - return fmt.Sprintf("jsonb_build_object(%s) || attributes", strings.Join(res, ",")) + return fmt.Sprintf("jsonb_build_object(%s) || %s.attributes", strings.Join(res, ","), table) } // nameToAttrPath takes a dotted string and converts it into a json field path. diff --git a/query/query_test.go b/query/query_test.go index 33519a4..56614d3 100644 --- a/query/query_test.go +++ b/query/query_test.go @@ -46,7 +46,18 @@ func TestJSONSelect(t *testing.T) { table string fields map[string]string out string - }{} + }{ + { + "zoo", + map[string]string{"foo": "foo", "bar": "bar", "zoo": "f.bar"}, + "jsonb_build_object('foo',foo,'bar',bar,'zoo',f.bar) || zoo.attributes", + }, + { + "f", + map[string]string{"foo": "l.attributes->>'foo'", "zoo": "a.zoo"}, + "jsonb_build_object('foo',l.attributes->>'foo','zoo',a.zoo) || f.attributes", + }, + } for _, test := range tests { out := FieldsToJSON(test.table, test.fields) if out != test.out {