From 0ebd13c966022051ff7ae32a1aab3576cfcac0a6 Mon Sep 17 00:00:00 2001 From: Kartikay Date: Sun, 23 Feb 2025 16:16:54 +0530 Subject: [PATCH] json output for schema Signed-off-by: Kartikay --- internal/cmd/preview.go | 2 +- internal/cmd/schema.go | 22 ++++++++++++++++++++-- internal/cmd/schema_test.go | 10 +++++----- 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/internal/cmd/preview.go b/internal/cmd/preview.go index 2e1dbd1b..374123cd 100644 --- a/internal/cmd/preview.go +++ b/internal/cmd/preview.go @@ -11,4 +11,4 @@ func registerPreviewCmd(rootCmd *cobra.Command) { } rootCmd.AddCommand(previewCmd) -} +} \ No newline at end of file diff --git a/internal/cmd/schema.go b/internal/cmd/schema.go index 031117fd..28e3de49 100644 --- a/internal/cmd/schema.go +++ b/internal/cmd/schema.go @@ -2,6 +2,7 @@ package cmd import ( "context" + "encoding/json" "errors" "fmt" "io" @@ -105,6 +106,7 @@ func registerAdditionalSchemaCmds(schemaCmd *cobra.Command) { schemaCmd.AddCommand(schemaCompileCmd) schemaCompileCmd.Flags().String("out", "", "output filepath; omitting writes to stdout") + schemaCompileCmd.Flags().Bool("json", false, "output schema as JSON") } func schemaDiffCmdFunc(_ *cobra.Command, args []string) error { @@ -392,6 +394,7 @@ func determinePrefixForSchema(ctx context.Context, specifiedPrefix string, clien func schemaCompileOuter(cmd *cobra.Command, args []string) (bool, error) { outputFilepath := cobrautil.MustGetString(cmd, "out") + asJson := cobrautil.MustGetBool(cmd, "json") var outputFile *os.File var toStdout bool @@ -413,12 +416,12 @@ func schemaCompileOuter(cmd *cobra.Command, args []string) (bool, error) { }() } - return toStdout, schemaCompileInner(cmd.Context(), args, outputFile) + return toStdout, schemaCompileInner(cmd.Context(), args, asJson, outputFile) } // Compiles an input schema written in the new composable schema syntax // and produces it as a fully-realized schema -func schemaCompileInner(ctx context.Context, args []string, writer io.Writer) error { +func schemaCompileInner(ctx context.Context, args []string, asJson bool, writer io.Writer) error { inputFilepath := args[0] inputSourceFolder := filepath.Dir(inputFilepath) schemaBytes, err := os.ReadFile(inputFilepath) @@ -449,6 +452,21 @@ func schemaCompileInner(ctx context.Context, args []string, writer io.Writer) er // Add a newline at the end for hygiene's sake terminated := generated + "\n" + if asJson { + output := struct { + SchemaText string `json:"schemaText"` + }{ + SchemaText: terminated, + } + + jsonOutput, err := json.Marshal(output) + if err != nil { + return err + } + + terminated = string(jsonOutput) + } + _, err = fmt.Fprint(writer, terminated) if err != nil { return fmt.Errorf("failed to write schema: %w", err) diff --git a/internal/cmd/schema_test.go b/internal/cmd/schema_test.go index ac83ce07..72e1c553 100644 --- a/internal/cmd/schema_test.go +++ b/internal/cmd/schema_test.go @@ -201,7 +201,7 @@ definition resource { var buf []byte writer := &testWriter{buffer: &buf} - err := schemaCompileInner(t.Context(), files, writer) + err := schemaCompileInner(t.Context(), files, false, writer) require.NoError(err) require.Equal(expected, string(buf)) @@ -213,7 +213,7 @@ func TestSchemaCompileFileNotFound(t *testing.T) { files := []string{filepath.Join("preview-test", "nonexistent.zed")} - err := schemaCompileInner(t.Context(), files, io.Discard) + err := schemaCompileInner(t.Context(), files, false, io.Discard) require.Error(err) require.ErrorIs(err, fs.ErrNotExist) } @@ -225,7 +225,7 @@ func TestSchemaCompileFailureFromReservedKeyword(t *testing.T) { files := []string{filepath.Join("preview-test", "composable-schema-invalid-root.zed")} var expectedErr compiler.BaseCompilerError - err := schemaCompileInner(t.Context(), files, io.Discard) + err := schemaCompileInner(t.Context(), files, false, io.Discard) require.Error(err) require.ErrorAs(err, &expectedErr) } @@ -236,7 +236,7 @@ func TestSchemaCompileWriteError(t *testing.T) { files := []string{filepath.Join("preview-test", "composable-schema-root.zed")} - err := schemaCompileInner(t.Context(), files, &failingWriter{err: errors.New("simulated write failure")}) + err := schemaCompileInner(t.Context(), files,false, &failingWriter{err: errors.New("simulated write failure")}) require.Error(err) require.ErrorContains(err, "failed to write schema") @@ -253,7 +253,7 @@ func TestSchemaCompileEmptySchema(t *testing.T) { files := []string{emptySchemaFile} - err = schemaCompileInner(t.Context(), files, io.Discard) + err = schemaCompileInner(t.Context(), files, false, io.Discard) require.Error(err) require.ErrorContains(err, "attempted to compile empty schema")