Skip to content

Commit

Permalink
Fix unmarshal anyOf from a json string (#53)
Browse files Browse the repository at this point in the history
* Fix unmarshal anyOf from a json string

* Fix pipelines
  • Loading branch information
nhatthm authored Jan 23, 2023
1 parent db4a8d1 commit 480040b
Show file tree
Hide file tree
Showing 14 changed files with 138 additions and 43 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ jobs:
with:
go-version: ${{ env.GO_VERSION }}

- id: get-lint-version
- id: vars
run: |
make golangci-lint-version
make $GITHUB_OUTPUT
- name: lint
uses: golangci/golangci-lint-action@v3
with:
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
version: ${{ steps.get-lint-version.outputs.GOLANGCI_LINT_VERSION }}
version: ${{ steps.vars.outputs.GOLANGCI_LINT_VERSION }}

# Optional: working directory, useful for monorepos
# working-directory: somedir
Expand Down
10 changes: 9 additions & 1 deletion .github/workflows/update-registry.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ name: 'update-registry'

on:
push:
branches:
- master
tags:
- v*
workflow_dispatch:
Expand All @@ -17,11 +19,17 @@ jobs:
matrix:
registry: [ go.nhat.io, go-staging.nhat.io ]
steps:
- uses: actions/checkout@v3

- id: vars
run: |
make $GITHUB_OUTPUT
- name: notify ${{ matrix.registry }}
uses: benc-uk/workflow-dispatch@v121
with:
workflow: build
repo: nhatthm/${{ matrix.registry }}
token: ${{ secrets.REGISTRY_TOKEN }}
inputs: '{"modules": "${{ env.MODULE_NAME }}"}'
inputs: '{"modules": "${{ steps.vars.outputs.MODULE_NAME }}"}'
ref: 'master'
18 changes: 13 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
BUILD_DIR ?= out
MODULE_NAME=grpcmock

VENDOR_DIR = vendor

GOLANGCI_LINT_VERSION ?= v1.48.0
GOLANGCI_LINT_VERSION ?= v1.50.1

GO ?= go
GOLANGCI_LINT ?= $(shell go env GOPATH)/bin/golangci-lint-$(GOLANGCI_LINT_VERSION)

GITHUB_OUTPUT ?= /dev/null

.PHONY: $(VENDOR_DIR)
$(VENDOR_DIR):
@mkdir -p $(VENDOR_DIR)
@$(GO) mod vendor
@$(GO) mod tidy

.PHONY: tidy
tidy:
@$(GO) mod tidy

.PHONY: lint
lint: $(GOLANGCI_LINT) $(VENDOR_DIR)
@$(GOLANGCI_LINT) run -c .golangci.yaml
Expand All @@ -35,9 +42,10 @@ gen:
@rm -rf test/grpctest
@protoc --go_out=. --go-grpc_out=. resources/protobuf/service.proto

.PHONY: golangci-lint-version
golangci-lint-version:
@echo "::set-output name=GOLANGCI_LINT_VERSION::$(GOLANGCI_LINT_VERSION)"
.PHONY: $(GITHUB_OUTPUT)
$(GITHUB_OUTPUT):
@echo "MODULE_NAME=$(MODULE_NAME)" >> "$@"
@echo "GOLANGCI_LINT_VERSION=$(GOLANGCI_LINT_VERSION)" >> "$@"

$(GOLANGCI_LINT):
@echo "$(OK_COLOR)==> Installing golangci-lint $(GOLANGCI_LINT_VERSION)$(NO_COLOR)"; \
Expand Down
24 changes: 24 additions & 0 deletions assert/assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package assert

import (
"encoding/json"
"strings"

"github.com/stretchr/testify/assert"
"github.com/swaggest/assertjson"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/proto"
)

Expand All @@ -17,6 +19,24 @@ func EqualMessage(t assert.TestingT, expected, actual proto.Message, msgAndArgs
return assert.Equal(t, expected, actual, msgAndArgs...)
}

// EqualError asserts that two grpc errors are equal.
func EqualError(t assert.TestingT, expected, actual error, msgAndArgs ...interface{}) bool {
if st, _ := status.FromError(actual); st != nil {
actual = status.Error(st.Code(), sanitizeErrorMessage(st.Message()))
}

return assert.Equal(t, expected, actual, msgAndArgs...)
}

// EqualErrorMessage asserts that the grpc error message is equal.
func EqualErrorMessage(t assert.TestingT, actual error, expected string, msgAndArgs ...interface{}) bool {
if st, _ := status.FromError(actual); st != nil {
actual = status.Error(st.Code(), sanitizeErrorMessage(st.Message()))
}

return assert.EqualError(t, actual, expected, msgAndArgs...)
}

// JSONEq compares 2 json objects.
func JSONEq(t assert.TestingT, expected, actual interface{}, msgAndArgs ...interface{}) bool {
if !assert.IsType(t, expected, actual) {
Expand All @@ -35,3 +55,7 @@ func JSONEq(t assert.TestingT, expected, actual interface{}, msgAndArgs ...inter

return assertjson.Equal(t, expectedBytes, actualBytes, msgAndArgs...)
}

func sanitizeErrorMessage(msg string) string {
return strings.ReplaceAll(msg, "\u00a0", " ")
}
12 changes: 6 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,20 @@ require (
)

require (
github.com/bool64/shared v0.1.4 // indirect
github.com/bool64/shared v0.1.5 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fsnotify/fsnotify v1.4.9 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/iancoleman/orderedmap v0.2.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/sergi/go-diff v1.2.0 // indirect
github.com/sergi/go-diff v1.3.1 // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/yudai/gojsondiff v1.0.0 // indirect
github.com/yudai/golcs v0.0.0-20170316035057-ecda9a501e82 // indirect
golang.org/x/net v0.4.0 // indirect
golang.org/x/sys v0.3.0 // indirect
golang.org/x/text v0.5.0 // indirect
google.golang.org/genproto v0.0.0-20221118155620-16455021b5e6 // indirect
golang.org/x/net v0.5.0 // indirect
golang.org/x/sys v0.4.0 // indirect
golang.org/x/text v0.6.0 // indirect
google.golang.org/genproto v0.0.0-20230119192704-9d59e20e5cd1 // indirect
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 480040b

Please sign in to comment.