Skip to content

Commit

Permalink
fix indentation, add simple tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lmbsog0 committed Jul 4, 2023
1 parent 1ffa4c5 commit 01f73e9
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 31 deletions.
42 changes: 39 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,47 @@ jobs:
name: grpc
path: |
pkg/a2l/protobuf
build-linux:
test-linux:
runs-on: ubuntu-22.04
needs:
- generate-antlr-sources
- generate-grpc-sources
steps:
- uses: actions/checkout@v3
- uses: actions/download-artifact@v3
with:
name: antlr4
path: pkg/a2l/parser
- uses: actions/download-artifact@v3
with:
name: grpc
path: pkg/a2l
- run: |
go get github.com/antlr4-go/antlr/v4
go test ./cmd/a2l/...
test-windows:
runs-on: windows-2019
needs:
- generate-antlr-sources
- generate-grpc-sources
steps:
- uses: actions/checkout@v3
- uses: actions/download-artifact@v3
with:
name: antlr4
path: pkg/a2l/parser
- uses: actions/download-artifact@v3
with:
name: grpc
path: pkg/a2l
- run: |
go get github.com/antlr4-go/antlr/v4
go test ./cmd/a2l/...
build-linux:
runs-on: ubuntu-22.04
needs:
- test-linux
- test-windows
steps:
- uses: actions/checkout@v3
- uses: actions/download-artifact@v3
Expand All @@ -50,8 +86,8 @@ jobs:
build-windows:
runs-on: windows-2019
needs:
- generate-antlr-sources
- generate-grpc-sources
- test-linux
- test-windows
steps:
- uses: actions/checkout@v3
- uses: actions/download-artifact@v3
Expand Down
38 changes: 11 additions & 27 deletions cmd/a2l/a2l.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package main

import (
"C"
"bytes"
"context"
"encoding/json"
"fmt"
"github.com/antlr4-go/antlr/v4"
"github.com/sauci/a2l-grpc/pkg/a2l"
Expand Down Expand Up @@ -86,17 +88,15 @@ func (s *grpcA2LImplType) GetTreeFromA2L(_ context.Context, request *a2l.TreeFro

func (s *grpcA2LImplType) GetJSONFromTree(_ context.Context, request *a2l.JSONFromTreeRequest) (result *a2l.JSONResponse, err error) {
var rawData []byte
var indentedData []byte
var parseError error
multiline := false
indent := ""
allowPartial := false
emitUnpopulated := false

result = &a2l.JSONResponse{}

if request.Indent != nil {
multiline = true

for i := uint32(0); i < *request.Indent; i++ {
indent += " "
}
Expand All @@ -111,34 +111,18 @@ func (s *grpcA2LImplType) GetJSONFromTree(_ context.Context, request *a2l.JSONFr
}

opt := protojson.MarshalOptions{
Multiline: multiline,
Indent: indent,
AllowPartial: allowPartial,
EmitUnpopulated: emitUnpopulated}

if rawData, parseError = opt.Marshal(request.Tree); parseError == nil {
result.Json = rawData
// fix https://github.com/golang/protobuf/issues/1121
//tmpResult := new(interface{})
//var stringResult []byte
//
//_ = json.Unmarshal(rawData, &tmpResult)
//
//if multiline {
// if stringResult, parseError = json.MarshalIndent(tmpResult, "", indent); parseError == nil {
// result.Json = string(stringResult)
// } else {
// errString := parseError.Error()
// result.Error = &errString
// }
//} else {
// if stringResult, parseError = json.Marshal(tmpResult); parseError == nil {
// result.Json = string(stringResult)
// } else {
// errString := parseError.Error()
// result.Error = &errString
// }
//}
// Note: see https://github.com/golang/protobuf/issues/1121
buffer := bytes.NewBuffer(indentedData)
if err = json.Indent(buffer, rawData, "", indent); err == nil {
result.Json = buffer.Bytes()
} else {
errString := err.Error()
result.Error = &errString
}
} else {
errString := parseError.Error()
result.Error = &errString
Expand Down
109 changes: 109 additions & 0 deletions cmd/a2l/a2l_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package main

import (
"github.com/sauci/a2l-grpc/pkg/a2l"
"github.com/stretchr/testify/assert"
"testing"
)

func baseTypeToPointer[T bool | uint32 | string](baseType T) *T {
return &baseType
}

func Test_GetJSONFromTree(t *testing.T) {
type testCaseType struct {
request *a2l.JSONFromTreeRequest
response *a2l.JSONResponse
}

for _, tc := range []testCaseType{
{
request: &a2l.JSONFromTreeRequest{
Tree: &a2l.RootNodeType{
PROJECT: &a2l.ProjectType{
Name: &a2l.IdentType{Value: "_"},
LongIdentifier: &a2l.StringType{Value: "_"},
},
},
Indent: baseTypeToPointer[uint32](1),
AllowPartial: baseTypeToPointer[bool](false),
EmitUnpopulated: baseTypeToPointer[bool](false),
},
response: &a2l.JSONResponse{
Json: []byte(`{
"PROJECT": {
"Name": {
"Value": "_"
},
"LongIdentifier": {
"Value": "_"
}
}
}`),
Error: nil,
},
},
{
request: &a2l.JSONFromTreeRequest{
Tree: &a2l.RootNodeType{
PROJECT: &a2l.ProjectType{
Name: &a2l.IdentType{Value: "_"},
LongIdentifier: &a2l.StringType{Value: "_"},
},
},
Indent: baseTypeToPointer[uint32](2),
AllowPartial: baseTypeToPointer[bool](false),
EmitUnpopulated: baseTypeToPointer[bool](false),
},
response: &a2l.JSONResponse{
Json: []byte(`{
"PROJECT": {
"Name": {
"Value": "_"
},
"LongIdentifier": {
"Value": "_"
}
}
}`),
Error: nil,
},
},
{
request: &a2l.JSONFromTreeRequest{
Tree: &a2l.RootNodeType{
PROJECT: &a2l.ProjectType{
Name: &a2l.IdentType{Value: "_"},
LongIdentifier: &a2l.StringType{Value: "_"},
},
},
Indent: baseTypeToPointer[uint32](3),
AllowPartial: baseTypeToPointer[bool](false),
EmitUnpopulated: baseTypeToPointer[bool](false),
},
response: &a2l.JSONResponse{
Json: []byte(`{
"PROJECT": {
"Name": {
"Value": "_"
},
"LongIdentifier": {
"Value": "_"
}
}
}`),
Error: nil,
},
},
} {
t.Run("", func(t *testing.T) {
grpc := grpcA2LImplType{}

if response, err := grpc.GetJSONFromTree(nil, tc.request); err == nil {
assert.Equal(t, tc.response, response)
} else {
t.Fatal(err)
}
})
}
}
1 change: 0 additions & 1 deletion protobuf/API.proto
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ message A2LResponse {

message JSONFromTreeRequest {
RootNodeType tree = 1;
bool sort_keys = 2;
optional uint32 indent = 3;
optional bool allow_partial = 4;
optional bool emit_unpopulated = 5;
Expand Down

0 comments on commit 01f73e9

Please sign in to comment.