Skip to content

Commit 0ebd13c

Browse files
kartikaysaxenamiparnisari
authored andcommitted
json output for schema
Signed-off-by: Kartikay <kartikay_2101ce32@iitp.ac.in>
1 parent cd9c974 commit 0ebd13c

3 files changed

Lines changed: 26 additions & 8 deletions

File tree

internal/cmd/preview.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ func registerPreviewCmd(rootCmd *cobra.Command) {
1111
}
1212

1313
rootCmd.AddCommand(previewCmd)
14-
}
14+
}

internal/cmd/schema.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cmd
22

33
import (
44
"context"
5+
"encoding/json"
56
"errors"
67
"fmt"
78
"io"
@@ -105,6 +106,7 @@ func registerAdditionalSchemaCmds(schemaCmd *cobra.Command) {
105106

106107
schemaCmd.AddCommand(schemaCompileCmd)
107108
schemaCompileCmd.Flags().String("out", "", "output filepath; omitting writes to stdout")
109+
schemaCompileCmd.Flags().Bool("json", false, "output schema as JSON")
108110
}
109111

110112
func schemaDiffCmdFunc(_ *cobra.Command, args []string) error {
@@ -392,6 +394,7 @@ func determinePrefixForSchema(ctx context.Context, specifiedPrefix string, clien
392394

393395
func schemaCompileOuter(cmd *cobra.Command, args []string) (bool, error) {
394396
outputFilepath := cobrautil.MustGetString(cmd, "out")
397+
asJson := cobrautil.MustGetBool(cmd, "json")
395398

396399
var outputFile *os.File
397400
var toStdout bool
@@ -413,12 +416,12 @@ func schemaCompileOuter(cmd *cobra.Command, args []string) (bool, error) {
413416
}()
414417
}
415418

416-
return toStdout, schemaCompileInner(cmd.Context(), args, outputFile)
419+
return toStdout, schemaCompileInner(cmd.Context(), args, asJson, outputFile)
417420
}
418421

419422
// Compiles an input schema written in the new composable schema syntax
420423
// and produces it as a fully-realized schema
421-
func schemaCompileInner(ctx context.Context, args []string, writer io.Writer) error {
424+
func schemaCompileInner(ctx context.Context, args []string, asJson bool, writer io.Writer) error {
422425
inputFilepath := args[0]
423426
inputSourceFolder := filepath.Dir(inputFilepath)
424427
schemaBytes, err := os.ReadFile(inputFilepath)
@@ -449,6 +452,21 @@ func schemaCompileInner(ctx context.Context, args []string, writer io.Writer) er
449452
// Add a newline at the end for hygiene's sake
450453
terminated := generated + "\n"
451454

455+
if asJson {
456+
output := struct {
457+
SchemaText string `json:"schemaText"`
458+
}{
459+
SchemaText: terminated,
460+
}
461+
462+
jsonOutput, err := json.Marshal(output)
463+
if err != nil {
464+
return err
465+
}
466+
467+
terminated = string(jsonOutput)
468+
}
469+
452470
_, err = fmt.Fprint(writer, terminated)
453471
if err != nil {
454472
return fmt.Errorf("failed to write schema: %w", err)

internal/cmd/schema_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ definition resource {
201201
var buf []byte
202202
writer := &testWriter{buffer: &buf}
203203

204-
err := schemaCompileInner(t.Context(), files, writer)
204+
err := schemaCompileInner(t.Context(), files, false, writer)
205205

206206
require.NoError(err)
207207
require.Equal(expected, string(buf))
@@ -213,7 +213,7 @@ func TestSchemaCompileFileNotFound(t *testing.T) {
213213

214214
files := []string{filepath.Join("preview-test", "nonexistent.zed")}
215215

216-
err := schemaCompileInner(t.Context(), files, io.Discard)
216+
err := schemaCompileInner(t.Context(), files, false, io.Discard)
217217
require.Error(err)
218218
require.ErrorIs(err, fs.ErrNotExist)
219219
}
@@ -225,7 +225,7 @@ func TestSchemaCompileFailureFromReservedKeyword(t *testing.T) {
225225
files := []string{filepath.Join("preview-test", "composable-schema-invalid-root.zed")}
226226
var expectedErr compiler.BaseCompilerError
227227

228-
err := schemaCompileInner(t.Context(), files, io.Discard)
228+
err := schemaCompileInner(t.Context(), files, false, io.Discard)
229229
require.Error(err)
230230
require.ErrorAs(err, &expectedErr)
231231
}
@@ -236,7 +236,7 @@ func TestSchemaCompileWriteError(t *testing.T) {
236236

237237
files := []string{filepath.Join("preview-test", "composable-schema-root.zed")}
238238

239-
err := schemaCompileInner(t.Context(), files, &failingWriter{err: errors.New("simulated write failure")})
239+
err := schemaCompileInner(t.Context(), files,false, &failingWriter{err: errors.New("simulated write failure")})
240240

241241
require.Error(err)
242242
require.ErrorContains(err, "failed to write schema")
@@ -253,7 +253,7 @@ func TestSchemaCompileEmptySchema(t *testing.T) {
253253

254254
files := []string{emptySchemaFile}
255255

256-
err = schemaCompileInner(t.Context(), files, io.Discard)
256+
err = schemaCompileInner(t.Context(), files, false, io.Discard)
257257

258258
require.Error(err)
259259
require.ErrorContains(err, "attempted to compile empty schema")

0 commit comments

Comments
 (0)