From 5426b7b3c86a9afc3fab855e2f3043d0efe658e4 Mon Sep 17 00:00:00 2001 From: Gregory Albouy <60700958+GregoryAlbouy@users.noreply.github.com> Date: Sun, 31 Oct 2021 18:39:11 +0100 Subject: [PATCH 01/19] feat(generics): create viable POC (#63) * Use go1.18 - Declare go 1.18 in go.mod - Update make commands * Declare generic checker types * Type checker providers using Checker[T] interface * Update checker providers interfaces - Disable code generation for providers interfaces - Update providers interfaces manually * Update checker providers unit tests * Update package check examples * Update package checkconv - Type checkconv.Assert using check.Checker[T] interface - Type checkconv.Cast using check.Checker[T] interface * Update testx runners - Add type parameters to ValueRunner - Use new type check.Checker[T] - Update tests and examples * Update temporary CI config - Disable jobs: gen, lint - Disable test coverage - Add setup step: Install gotip * Replace all occurrences of interface{} -> any --- .circleci/config.yml | 169 ++++++++++---------- .golangci.yml | 6 +- Makefile | 8 +- check/README.md | 8 +- check/check.go | 14 +- check/check2.go | 36 +++++ check/checkers.go | 8 +- check/example_custom_closures_test.go | 15 +- check/example_custom_test.go | 14 +- check/example_custom_unknown_test.go | 2 +- check/example_newintchecker_test.go | 7 +- check/gen.go | 4 +- check/providers.go | 193 +++++++++++------------ check/providers_base.go | 32 ++-- check/providers_base_internal_test.go | 16 +- check/providers_bool.go | 6 +- check/providers_bool_test.go | 32 +--- check/providers_bytes.go | 62 ++++---- check/providers_bytes_test.go | 74 +++------ check/providers_context.go | 20 +-- check/providers_context_test.go | 45 ++---- check/providers_duration.go | 24 +-- check/providers_duration_test.go | 50 ++---- check/providers_float64.go | 48 +++--- check/providers_float64_test.go | 70 +++----- check/providers_httpheader.go | 34 ++-- check/providers_httpheader_test.go | 50 ++---- check/providers_httprequest.go | 46 +++--- check/providers_httprequest_test.go | 50 ++---- check/providers_httpresponse.go | 56 +++---- check/providers_httpresponse_test.go | 54 ++----- check/providers_int.go | 48 +++--- check/providers_int_test.go | 70 +++----- check/providers_map.go | 62 ++++---- check/providers_map_test.go | 82 ++++------ check/providers_slice.go | 84 +++++----- check/providers_slice_test.go | 68 +++----- check/providers_string.go | 46 +++--- check/providers_string_test.go | 56 ++----- check/providers_struct.go | 28 ++-- check/providers_struct_test.go | 36 ++--- check/providers_test.go | 50 ++++-- check/providers_value.go | 52 +++--- check/providers_value_test.go | 60 +++---- checkconv/README.md | 16 +- checkconv/assert.go | 175 ++++++++++---------- checkconv/assert_test.go | 12 +- checkconv/cast.go | 12 +- checkconv/cast_test.go | 8 +- checkconv/checkconv_test.go | 40 ++--- checkconv/gen.go | 2 +- checkconv/ischecker.go | 4 +- checkconv/ischecker_test.go | 2 +- example_httphandler_test.go | 2 +- example_table_test.go | 6 +- example_value_test.go | 11 +- go.mod | 2 +- internal/fmtexpl/fmtexpl.go | 4 +- internal/gen/gen.go | 8 +- internal/gen/templates/assert.gotmpl | 36 +++-- internal/gen/templates/check.gotmpl | 4 +- internal/gen/templates/checkers.gotmpl | 2 +- internal/gen/types.go | 2 +- internal/reflectutil/func.go | 6 +- internal/reflectutil/func_test.go | 2 +- internal/reflectutil/reflectutil.go | 14 +- internal/reflectutil/reflectutil_test.go | 16 +- internal/testutil/testutil.go | 2 +- runner.go | 6 +- runner_httphandler.go | 6 +- runner_httphandler_test.go | 6 +- runner_table.go | 22 +-- runner_table_test.go | 20 +-- runner_test.go | 6 +- runner_value.go | 36 +++-- runner_value_test.go | 12 +- testx.go | 18 +-- 77 files changed, 1164 insertions(+), 1351 deletions(-) create mode 100644 check/check2.go diff --git a/.circleci/config.yml b/.circleci/config.yml index 5ada2f0..2dce924 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -17,110 +17,115 @@ commands: name: Install dependencies command: | go mod download + - run: + name: Install gotip + command: | + go install golang.org/dl/gotip@latest + gotip download - save_cache: key: go-mod-v4-{{ checksum "go.sum" }}-{{ .Environment.CACHE_VERSION }} paths: - "/go/pkg/mod" - setup-lint: - description: Set up lint - parameters: - version: - type: string - default: v1.40.1 - steps: - - restore_cache: - keys: - - golangci-lint-<< parameters.version >>-{{ .Environment.CACHE_VERSION }} - - golangci-lint-{{ .Environment.CACHE_VERSION }} - - run: - name: Install golangci-lint - command: | - command -v /go/bin/golangci-lint && echo "Skipping golangci-lint installation" && exit - curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh| sh -s -- -b /go/bin << parameters.version >> - - save_cache: - key: golangci-lint-<< parameters.version >>-{{ .Environment.CACHE_VERSION }} - paths: - - /go/bin/golangci-lint + # setup-lint: + # description: Set up lint + # parameters: + # version: + # type: string + # default: v1.40.1 + # steps: + # - restore_cache: + # keys: + # - golangci-lint-<< parameters.version >>-{{ .Environment.CACHE_VERSION }} + # - golangci-lint-{{ .Environment.CACHE_VERSION }} + # - run: + # name: Install golangci-lint + # command: | + # command -v /go/bin/golangci-lint && echo "Skipping golangci-lint installation" && exit + # curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh| sh -s -- -b /go/bin << parameters.version >> + # - save_cache: + # key: golangci-lint-<< parameters.version >>-{{ .Environment.CACHE_VERSION }} + # paths: + # - /go/bin/golangci-lint - setup-coverage: - description: Set up coverage - steps: - - restore_cache: - keys: - - go-acc-{{ .Environment.CACHE_VERSION }} - - run: - name: Install go-acc - command: | - command -v /go/bin/go-acc && echo "Skipping go-acc installation" && exit - go get -v github.com/ory/go-acc - - save_cache: - key: go-acc-{{ .Environment.CACHE_VERSION }} - paths: - - /go/bin/go-acc + # setup-coverage: + # description: Set up coverage + # steps: + # - restore_cache: + # keys: + # - go-acc-{{ .Environment.CACHE_VERSION }} + # - run: + # name: Install go-acc + # command: | + # command -v /go/bin/go-acc && echo "Skipping go-acc installation" && exit + # go get -v github.com/ory/go-acc + # - save_cache: + # key: go-acc-{{ .Environment.CACHE_VERSION }} + # paths: + # - /go/bin/go-acc - setup-gen: - description: Set up gen - steps: - - restore_cache: - keys: - - goimports-{{ .Environment.CACHE_VERSION }} - - run: - name: Install goimports - command: | - command -v /go/bin/goimports && echo "Skipping go-acc installation" && exit - go get -v golang.org/x/tools/cmd/goimports + # setup-gen: + # description: Set up gen + # steps: + # - restore_cache: + # keys: + # - goimports-{{ .Environment.CACHE_VERSION }} + # - run: + # name: Install goimports + # command: | + # command -v /go/bin/goimports && echo "Skipping go-acc installation" && exit + # go get -v golang.org/x/tools/cmd/goimports - - save_cache: - key: goimports-{{ .Environment.CACHE_VERSION }} - paths: - - /go/bin/goimports + # - save_cache: + # key: goimports-{{ .Environment.CACHE_VERSION }} + # paths: + # - /go/bin/goimports jobs: - lint: - <<: *defaults - steps: - - setup - - setup-lint - - run: - name: Run linters - command: | - make lint + # lint: + # <<: *defaults + # steps: + # - setup + # - setup-lint + # - run: + # name: Run linters + # command: | + # make lint test: <<: *defaults steps: - setup - - setup-coverage + # - setup-coverage - run: name: Run unit tests - command: | - make test-cov - - run: - name: Send test coverage results - command: | - bash <(curl -s https://codecov.io/bash) + command: | # TODO: set back to 'make test-cov' + make tests + # - run: + # name: Send test coverage results + # command: | + # bash <(curl -s https://codecov.io/bash) - gen: - <<: *defaults - steps: - - setup - - setup-gen - - setup-lint - - run: - name: Run code gen - command: | - make gen - - run: - name: Check generated code - command: | - make lint tests + # gen: + # <<: *defaults + # steps: + # - setup + # - setup-gen + # - setup-lint + # - run: + # name: Run code gen + # command: | + # make gen + # - run: + # name: Check generated code + # command: | + # make lint tests workflows: version: 2 main: jobs: - - lint + # - lint - test - - gen + # - gen diff --git a/.golangci.yml b/.golangci.yml index db2f26b..de88a52 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -104,7 +104,7 @@ linters-settings: sizeThreshold: 256 gofumpt: - lang-version: "1.16" + lang-version: "1.18" extra-rules: true goimports: @@ -117,11 +117,11 @@ linters-settings: enableAllRules: true staticcheck: - go: "1.16" + go: "1.18" checks: [all] stylecheck: - go: "1.16" + go: "1.18" checks: [all] linters: diff --git a/Makefile b/Makefile index 1142a52..b1a26ff 100644 --- a/Makefile +++ b/Makefile @@ -9,9 +9,9 @@ default: .PHONY: gen gen: @echo "🛠 Building gen binary" - @go build -o ./bin/gen ./cmd/gen/main.go + @gotip build -o ./bin/gen ./cmd/gen/main.go @echo "✅ Done\n" - @go generate ./... + @gotip generate ./... # Check code @@ -26,7 +26,7 @@ lint: .PHONY: tests tests: - @go test ./... + @gotip test ./... .PHONY: test-cov test-cov: # Unit tests with coverage, excluding gen-related files @@ -43,7 +43,7 @@ endif .PHONY: test test: - @go test -timeout 30s -run $(TEST_FUNC) $(TEST_PKG) + @gotip test -timeout 30s -run $(TEST_FUNC) $(TEST_PKG) # Docs diff --git a/check/README.md b/check/README.md index 8122158..6969e86 100644 --- a/check/README.md +++ b/check/README.md @@ -39,7 +39,7 @@ type IntPasser interface { Pass(got int) bool } // Explainer provides a method Explain to describe the reason of a failed check. -type Explainer interface { Explain(label string, got interface{}) string } +type Explainer interface { Explain(label string, got any) string } ``` ## Provided checkers @@ -148,7 +148,7 @@ Package `check` provides a collection of basic checkers for common types and kin
interface{}anycheck.Value| testx | -testx/check | -testx/checkconv | -
| File | -Use case | -
|---|---|
check/check.go |
- Checkers types declarations | -
check/checkers.go |
- Checkers types declarations | -
check/providers.go |
- Checkers providers interfaces | -
checkconv/assert.go |
- Checkers types declarations | -
boolcheck.Boolint, intN (8, 16, 32, 64)check.Int, check.IntNTypeCheckerProvider
+
+ NumberCheckerProvider[int], NumberCheckerProvider[intN]
intcheck.Intuint, uintN (8, 16, 32, 64)check.Uint, check.UintNIntCheckerProvider
+
+ NumberCheckerProvider[uint], NumberCheckerProvider[uintN]
float64check.Float64floatN (32, 64)check.FloatNFloat64CheckerProvider
+
+ NumberCheckerProvider[floatN]
+
+ boolcheck.BoolBoolCheckerProvider
structcheck.Boolcheck.StructStructCheckerProvider
@@ -205,14 +205,15 @@ There are 2 ways to extend a provided checker.
#### Using a `New` function
-With every `