From f96f8ba4ed747090d3860f3a4431d7bf3b58c844 Mon Sep 17 00:00:00 2001 From: Gibheer Date: Fri, 7 May 2021 11:35:48 +0200 Subject: [PATCH] fix jsonb output generator When selecting content of a jsonb field the type per default is jsonb. But we need to proper posgres type, so that the output can be parsed properly. Therefore make sure that the field has the proper output operator attached. --- query/query.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/query/query.go b/query/query.go index bc17f26..5d1ab98 100644 --- a/query/query.go +++ b/query/query.go @@ -28,9 +28,9 @@ func FieldListToSelect(tableName string, fl types.FieldList, nameMap map[string] res := []string{} for _, name := range fl.Fields() { if field, found := nameMap[name]; found { - res = append(res, fmt.Sprintf(`%s as %s`, field, name)) + res = append(res, fmt.Sprintf(`%s as "%s"`, field, name)) } else { - res = append(res, fmt.Sprintf(`%s as %s`, nameToAttrPath(tableName, name), name)) + res = append(res, fmt.Sprintf(`%s as "%s"`, nameToAttrPath(tableName, name), name)) } } return strings.Join(res, ",") @@ -45,7 +45,12 @@ func nameToAttrPath(tabName, name string) string { for i, part := range parts { parts[i] = fmt.Sprintf(`'%s'`, part) } - return fmt.Sprintf("%s.attributes->%s", tabName, strings.Join(parts, "->")) + path := fmt.Sprintf("%s.attributes", tabName) + if len(parts) > 1 { + path += fmt.Sprintf("->%s", strings.Join(parts[:len(parts)-1], "->")) + } + path += fmt.Sprintf("->>%s", parts[len(parts)-1]) + return path } // FieldMapToUpdate generates the necessary elements for an update.