Skip to content

Commit

Permalink
Added proper result structure for diagnostics
Browse files Browse the repository at this point in the history
  • Loading branch information
cmaglie committed Nov 7, 2023
1 parent 3190022 commit 57ad932
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 10 deletions.
19 changes: 9 additions & 10 deletions internal/cli/compile/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ func runCompileCommand(cmd *cobra.Command, args []string) {
UpdatedUploadPort: result.NewPort(uploadRes.GetUpdatedUploadPort()),
},
ProfileOut: profileOut,
Diagnostics: compileRes.GetDiagnostics(),
Diagnostics: result.NewCompileDiagnostics(compileRes.GetDiagnostics()),
Success: compileError == nil,
showPropertiesMode: showProperties,
hideStats: preprocess,
Expand Down Expand Up @@ -393,15 +393,14 @@ type updatedUploadPortResult struct {
}

type compileResult struct {
CompilerOut string `json:"compiler_out"`
CompilerErr string `json:"compiler_err"`
BuilderResult *result.CompileResponse `json:"builder_result"`
UploadResult updatedUploadPortResult `json:"upload_result"`
Success bool `json:"success"`
ProfileOut string `json:"profile_out,omitempty"`
Error string `json:"error,omitempty"`
// TODO: make a proper result.* structure
Diagnostics []*rpc.CompileDiagnostic `json:"diagnostics"`
CompilerOut string `json:"compiler_out"`
CompilerErr string `json:"compiler_err"`
BuilderResult *result.CompileResponse `json:"builder_result"`
UploadResult updatedUploadPortResult `json:"upload_result"`
Success bool `json:"success"`
ProfileOut string `json:"profile_out,omitempty"`
Error string `json:"error,omitempty"`
Diagnostics []*result.CompileDiagnostic `json:"diagnostics"`
showPropertiesMode arguments.ShowPropertiesMode
hideStats bool
}
Expand Down
61 changes: 61 additions & 0 deletions internal/cli/feedback/result/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package result
import (
"cmp"

f "github.com/arduino/arduino-cli/internal/algorithms"
"github.com/arduino/arduino-cli/internal/orderedmap"
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
semver "go.bug.st/relaxed-semver"
Expand Down Expand Up @@ -880,6 +881,7 @@ type CompileResponse struct {
BoardPlatform *InstalledPlatformReference `json:"board_platform,omitempty"`
BuildPlatform *InstalledPlatformReference `json:"build_platform,omitempty"`
BuildProperties []string `json:"build_properties,omitempty"`
Diagnostics []*CompileDiagnostic `json:"diagnostics,omitempty"`
}

func NewCompileResponse(c *rpc.CompileResponse) *CompileResponse {
Expand All @@ -904,6 +906,7 @@ func NewCompileResponse(c *rpc.CompileResponse) *CompileResponse {
BoardPlatform: NewInstalledPlatformReference(c.GetBoardPlatform()),
BuildPlatform: NewInstalledPlatformReference(c.GetBuildPlatform()),
BuildProperties: c.GetBuildProperties(),
Diagnostics: NewCompileDiagnostics(c.GetDiagnostics()),
}
}

Expand Down Expand Up @@ -959,3 +962,61 @@ func NewBoardListWatchResponse(r *rpc.BoardListWatchResponse) *BoardListWatchRes
Error: r.Error,
}
}

type CompileDiagnostic struct {
Severity string `json:"severity,omitempty"`
Message string `json:"message,omitempty"`
File string `json:"file,omitempty"`
Line int64 `json:"line,omitempty"`
Column int64 `json:"column,omitempty"`
Context []*CompileDiagnosticContext `json:"context,omitempty"`
Notes []*CompileDiagnosticNote `json:"notes,omitempty"`
}

func NewCompileDiagnostics(cd []*rpc.CompileDiagnostic) []*CompileDiagnostic {
return f.Map(cd, NewCompileDiagnostic)
}

func NewCompileDiagnostic(cd *rpc.CompileDiagnostic) *CompileDiagnostic {
return &CompileDiagnostic{
Severity: cd.GetSeverity(),
Message: cd.GetMessage(),
File: cd.GetFile(),
Line: cd.GetLine(),
Column: cd.GetColumn(),
Context: f.Map(cd.GetContext(), NewCompileDiagnosticContext),
Notes: f.Map(cd.GetNotes(), NewCompileDiagnosticNote),
}
}

type CompileDiagnosticContext struct {
Message string `json:"message,omitempty"`
File string `json:"file,omitempty"`
Line int64 `json:"line,omitempty"`
Column int64 `json:"column,omitempty"`
}

func NewCompileDiagnosticContext(cdc *rpc.CompileDiagnosticContext) *CompileDiagnosticContext {
return &CompileDiagnosticContext{
Message: cdc.GetMessage(),
File: cdc.GetFile(),
Line: cdc.GetLine(),
Column: cdc.GetColumn(),
}
}

type CompileDiagnosticNote struct {
Message string `json:"message,omitempty"`
File string `json:"file,omitempty"`
Line int64 `json:"line,omitempty"`
Column int64 `json:"column,omitempty"`
}

func NewCompileDiagnosticNote(cdn *rpc.CompileDiagnosticNote) *CompileDiagnosticNote {
return &CompileDiagnosticNote{
Message: cdn.GetMessage(),
File: cdn.GetFile(),
Line: cdn.GetLine(),
Column: cdn.GetColumn(),
}
}
12 changes: 12 additions & 0 deletions internal/cli/feedback/result/rpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,18 @@ func TestAllFieldAreMapped(t *testing.T) {
boardListWatchResponseRpc := &rpc.BoardListWatchResponse{}
boardListWatchResponseResult := result.NewBoardListWatchResponse(boardListWatchResponseRpc)
mustContainsAllPropertyOfRpcStruct(t, boardListWatchResponseRpc, boardListWatchResponseResult)

compileDiagnosticRpc := &rpc.CompileDiagnostic{}
compileDiagnosticResult := result.NewCompileDiagnostic(compileDiagnosticRpc)
mustContainsAllPropertyOfRpcStruct(t, compileDiagnosticRpc, compileDiagnosticResult)

compileDiagnosticContextRpc := &rpc.CompileDiagnosticContext{}
compileDiagnosticContextResult := result.NewCompileDiagnosticContext(compileDiagnosticContextRpc)
mustContainsAllPropertyOfRpcStruct(t, compileDiagnosticContextRpc, compileDiagnosticContextResult)

compileDiagnosticNoteRpc := &rpc.CompileDiagnosticNote{}
compileDiagnosticNoteResult := result.NewCompileDiagnosticNote(compileDiagnosticNoteRpc)
mustContainsAllPropertyOfRpcStruct(t, compileDiagnosticNoteRpc, compileDiagnosticNoteResult)
}

func TestEnumsMapsEveryRpcCounterpart(t *testing.T) {
Expand Down

0 comments on commit 57ad932

Please sign in to comment.