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 {