@@ -153,3 +153,49 @@ func TestOutputColumnsJSON(t *testing.T) {
153153 }
154154 })
155155}
156+
157+ func TestOutputColumnsEmbedJSON (t * testing.T ) {
158+ c := newTestCompiler (t , testSchema )
159+
160+ t .Run ("scalar mirrors the table's columns" , func (t * testing.T ) {
161+ a := mustAnalyze (t , c , `SELECT sqlc.embed.jsonb(items) AS obj FROM items;` )
162+ col := a .Columns [0 ]
163+ if col .JSONName != "obj" {
164+ t .Errorf ("JSONName = %q, want %q" , col .JSONName , "obj" )
165+ }
166+ if col .IsArray {
167+ t .Errorf ("IsArray = true, want false" )
168+ }
169+ if ! col .NotNull {
170+ t .Errorf ("NotNull = false, want true" )
171+ }
172+ if len (col .JSONFields ) != 3 {
173+ t .Fatalf ("expected 3 JSONFields (id, x, y), got %d" , len (col .JSONFields ))
174+ }
175+ names := []string {col .JSONFields [0 ].Name , col .JSONFields [1 ].Name , col .JSONFields [2 ].Name }
176+ if names [0 ] != "id" || names [1 ] != "x" || names [2 ] != "y" {
177+ t .Errorf ("field names = %v, want [id x y]" , names )
178+ }
179+ })
180+
181+ t .Run ("array singularizes the outer alias for the element name" , func (t * testing.T ) {
182+ a := mustAnalyze (t , c , `SELECT ARRAY(SELECT sqlc.embed.jsonb(items) FROM items) AS objs;` )
183+ col := a .Columns [0 ]
184+ if ! col .IsArray || col .ArrayDims != 1 {
185+ t .Errorf ("IsArray/ArrayDims = %v/%d, want true/1" , col .IsArray , col .ArrayDims )
186+ }
187+ if col .JSONName != "obj" {
188+ t .Errorf ("JSONName = %q, want %q (singular of the alias)" , col .JSONName , "obj" )
189+ }
190+ if len (col .JSONFields ) != 3 {
191+ t .Fatalf ("expected 3 JSONFields, got %d" , len (col .JSONFields ))
192+ }
193+ })
194+
195+ t .Run ("unknown table" , func (t * testing.T ) {
196+ err := analyzeErr (t , c , `SELECT sqlc.embed.jsonb(missing) AS obj FROM items;` )
197+ if ! strings .Contains (err .Error (), "table not found" ) {
198+ t .Errorf ("error = %v, want mention of table not found" , err )
199+ }
200+ })
201+ }
0 commit comments