Skip to content

Commit

Permalink
feat: update module
Browse files Browse the repository at this point in the history
- Bump minimal go version to 1.21.0
- Run rekres
- Deprecate `context.RecvWithContext`. Remove it in the next version.
- Deprecate `slices`. Move stuff that doesn't exist in `slices` yet to `xslices`.
- Remove `xsync`. Only one update in `runtime` is required.
- Add `ensure` package that provides wrappers function results wrappers that panics on error.
- Add `must` package that does pretty much the same for tests.
- Add `check` package that provides a set functions for table-driven tests.

Signed-off-by: Dmitriy Matrenichev <[email protected]>
  • Loading branch information
DmitriyMV committed Aug 22, 2023
1 parent f9f5805 commit 36a3ae3
Show file tree
Hide file tree
Showing 20 changed files with 541 additions and 239 deletions.
6 changes: 4 additions & 2 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
# THIS FILE WAS AUTOMATICALLY GENERATED, PLEASE DO NOT EDIT.
#
# Generated on 2022-11-28T15:27:14Z by kres 3ac53a8.
# Generated on 2023-08-21T16:35:57Z by kres latest.

*
!channel
!containers
!ensure
!maps
!optional
!pair
!slices
!value
!xerrors
!xsync
!xslices
!xtesting
!go.mod
!go.sum
!.golangci.yml
Expand Down
4 changes: 2 additions & 2 deletions .drone.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
# THIS FILE WAS AUTOMATICALLY GENERATED, PLEASE DO NOT EDIT.
#
# Generated on 2023-05-11T13:47:02Z by kres latest.
# Generated on 2023-08-21T14:49:21Z by kres latest.

kind: pipeline
type: kubernetes
Expand Down Expand Up @@ -174,7 +174,7 @@ steps:

services:
- name: docker
image: docker:23.0-dind
image: docker:24.0-dind
entrypoint:
- dockerd
commands:
Expand Down
17 changes: 11 additions & 6 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# THIS FILE WAS AUTOMATICALLY GENERATED, PLEASE DO NOT EDIT.
#
# Generated on 2023-05-11T13:47:02Z by kres latest.
# Generated on 2023-08-21T14:49:21Z by kres latest.

# options for analysis running
run:
Expand Down Expand Up @@ -36,7 +36,7 @@ linters-settings:
lines: 60
statements: 40
gci:
local-prefixes: github.com/siderolabs/gen
local-prefixes: github.com/siderolabs/gen/
gocognit:
min-complexity: 30
ireturn:
Expand Down Expand Up @@ -65,7 +65,7 @@ linters-settings:
gofmt:
simplify: true
goimports:
local-prefixes: github.com/siderolabs/gen
local-prefixes: github.com/siderolabs/gen/
golint:
min-confidence: 0.8
gomnd:
Expand All @@ -74,9 +74,8 @@ linters-settings:
govet:
check-shadowing: true
enable-all: true
depguard:
list-type: blacklist
include-go-root: false
disable:
- loopclosure
lll:
line-length: 200
tab-width: 4
Expand Down Expand Up @@ -118,6 +117,10 @@ linters-settings:
cyclop:
# the maximal code complexity to report
max-complexity: 20
# depguard:
# Main:
# deny:
# - github.com/OpenPeeDeeP/depguard # this is just an example

linters:
enable-all: true
Expand Down Expand Up @@ -145,6 +148,8 @@ linters:
- typecheck
- varnamelen
- wrapcheck
- depguard # Disabled because starting with golangci-lint 1.53.0 it doesn't allow denylist alone anymore
- tagalign
# abandoned linters for which golangci shows the warning that the repo is archived by the owner
- interfacer
- maligned
Expand Down
41 changes: 26 additions & 15 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
# syntax = docker/dockerfile-upstream:1.5.2-labs
# syntax = docker/dockerfile-upstream:1.6.0-labs

# THIS FILE WAS AUTOMATICALLY GENERATED, PLEASE DO NOT EDIT.
#
# Generated on 2023-05-11T13:47:02Z by kres latest.
# Generated on 2023-08-21T16:35:57Z by kres latest.

ARG TOOLCHAIN

# cleaned up specs and compiled versions
FROM scratch AS generate

# runs markdownlint
FROM docker.io/node:20.1.0-alpine3.17 AS lint-markdown
FROM docker.io/node:20.5.1-alpine3.18 AS lint-markdown
WORKDIR /src
RUN npm i -g markdownlint-cli@0.34.0
RUN npm i -g markdownlint-cli@0.35.0
RUN npm i [email protected]
COPY .markdownlint.json .
COPY ./README.md ./README.md
Expand All @@ -27,38 +27,45 @@ FROM --platform=${BUILDPLATFORM} toolchain AS tools
ENV GO111MODULE on
ARG CGO_ENABLED
ENV CGO_ENABLED ${CGO_ENABLED}
ARG GOTOOLCHAIN
ENV GOTOOLCHAIN ${GOTOOLCHAIN}
ARG GOEXPERIMENT
ENV GOEXPERIMENT ${GOEXPERIMENT}
ENV GOPATH /go
ARG DEEPCOPY_VERSION
RUN --mount=type=cache,target=/root/.cache/go-build --mount=type=cache,target=/go/pkg go install github.com/siderolabs/deep-copy@${DEEPCOPY_VERSION} \
&& mv /go/bin/deep-copy /bin/deep-copy
ARG GOLANGCILINT_VERSION
RUN --mount=type=cache,target=/root/.cache/go-build --mount=type=cache,target=/go/pkg go install github.com/golangci/golangci-lint/cmd/golangci-lint@${GOLANGCILINT_VERSION} \
&& mv /go/bin/golangci-lint /bin/golangci-lint
ARG GOFUMPT_VERSION
RUN go install mvdan.cc/gofumpt@${GOFUMPT_VERSION} \
&& mv /go/bin/gofumpt /bin/gofumpt
RUN --mount=type=cache,target=/root/.cache/go-build --mount=type=cache,target=/go/pkg go install golang.org/x/vuln/cmd/govulncheck@latest \
&& mv /go/bin/govulncheck /bin/govulncheck
ARG GOIMPORTS_VERSION
RUN --mount=type=cache,target=/root/.cache/go-build --mount=type=cache,target=/go/pkg go install golang.org/x/tools/cmd/goimports@${GOIMPORTS_VERSION} \
&& mv /go/bin/goimports /bin/goimports
ARG DEEPCOPY_VERSION
RUN --mount=type=cache,target=/root/.cache/go-build --mount=type=cache,target=/go/pkg go install github.com/siderolabs/deep-copy@${DEEPCOPY_VERSION} \
&& mv /go/bin/deep-copy /bin/deep-copy
ARG GOFUMPT_VERSION
RUN go install mvdan.cc/gofumpt@${GOFUMPT_VERSION} \
&& mv /go/bin/gofumpt /bin/gofumpt

# tools and sources
FROM tools AS base
WORKDIR /src
COPY ./go.mod .
COPY ./go.sum .
COPY go.mod go.mod
COPY go.sum go.sum
RUN cd .
RUN --mount=type=cache,target=/go/pkg go mod download
RUN --mount=type=cache,target=/go/pkg go mod verify
COPY ./channel ./channel
COPY ./containers ./containers
COPY ./ensure ./ensure
COPY ./maps ./maps
COPY ./optional ./optional
COPY ./pair ./pair
COPY ./slices ./slices
COPY ./value ./value
COPY ./xerrors ./xerrors
COPY ./xsync ./xsync
COPY ./xslices ./xslices
COPY ./xtesting ./xtesting
RUN --mount=type=cache,target=/go/pkg go list -mod=readonly all >/dev/null

# runs gofumpt
Expand All @@ -67,28 +74,32 @@ RUN FILES="$(gofumpt -l .)" && test -z "${FILES}" || (echo -e "Source code is no

# runs goimports
FROM base AS lint-goimports
RUN FILES="$(goimports -l -local github.com/siderolabs/gen .)" && test -z "${FILES}" || (echo -e "Source code is not formatted with 'goimports -w -local github.com/siderolabs/gen .':\n${FILES}"; exit 1)
RUN FILES="$(goimports -l -local github.com/siderolabs/gen/ .)" && test -z "${FILES}" || (echo -e "Source code is not formatted with 'goimports -w -local github.com/siderolabs/gen/ .':\n${FILES}"; exit 1)

# runs golangci-lint
FROM base AS lint-golangci-lint
WORKDIR /src
COPY .golangci.yml .
ENV GOGC 50
RUN --mount=type=cache,target=/root/.cache/go-build --mount=type=cache,target=/root/.cache/golangci-lint --mount=type=cache,target=/go/pkg golangci-lint run --config .golangci.yml

# runs govulncheck
FROM base AS lint-govulncheck
WORKDIR /src
RUN --mount=type=cache,target=/root/.cache/go-build --mount=type=cache,target=/go/pkg govulncheck ./...

# runs unit-tests with race detector
FROM base AS unit-tests-race
WORKDIR /src
ARG TESTPKGS
RUN --mount=type=cache,target=/root/.cache/go-build --mount=type=cache,target=/go/pkg --mount=type=cache,target=/tmp CGO_ENABLED=1 go test -v -race -count 1 ${TESTPKGS}

# runs unit-tests
FROM base AS unit-tests-run
WORKDIR /src
ARG TESTPKGS
RUN --mount=type=cache,target=/root/.cache/go-build --mount=type=cache,target=/go/pkg --mount=type=cache,target=/tmp go test -v -covermode=atomic -coverprofile=coverage.txt -coverpkg=${TESTPKGS} -count 1 ${TESTPKGS}

FROM scratch AS unit-tests
COPY --from=unit-tests-run /src/coverage.txt /coverage.txt
COPY --from=unit-tests-run /src/coverage.txt /coverage-unit-tests.txt

32 changes: 20 additions & 12 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,30 +1,33 @@
# THIS FILE WAS AUTOMATICALLY GENERATED, PLEASE DO NOT EDIT.
#
# Generated on 2023-05-11T13:47:02Z by kres latest.
# Generated on 2023-08-21T14:49:21Z by kres latest.

# common variables

SHA := $(shell git describe --match=none --always --abbrev=8 --dirty)
TAG := $(shell git describe --tag --always --dirty)
ABBREV_TAG := $(shell git describe --tags >/dev/null 2>/dev/null && git describe --tag --always --match v[0-9]\* --abbrev=0 || echo 'undefined')
BRANCH := $(shell git rev-parse --abbrev-ref HEAD)
ARTIFACTS := _out
WITH_DEBUG ?= false
WITH_RACE ?= false
REGISTRY ?= ghcr.io
USERNAME ?= siderolabs
REGISTRY_AND_USERNAME ?= $(REGISTRY)/$(USERNAME)
GOLANGCILINT_VERSION ?= v1.52.2
GOFUMPT_VERSION ?= v0.5.0
GO_VERSION ?= 1.20
GOIMPORTS_VERSION ?= v0.9.1
PROTOBUF_GO_VERSION ?= 1.28.1
GRPC_GO_VERSION ?= 1.3.0
GRPC_GATEWAY_VERSION ?= 2.15.2
GRPC_GATEWAY_VERSION ?= 2.16.2
VTPROTOBUF_VERSION ?= 0.4.0
DEEPCOPY_VERSION ?= v0.5.5
GOLANGCILINT_VERSION ?= v1.54.2
GOFUMPT_VERSION ?= v0.5.0
GO_VERSION ?= 1.21
GOIMPORTS_VERSION ?= v0.12.0
GO_BUILDFLAGS ?=
GO_LDFLAGS ?=
CGO_ENABLED ?= 0
GOTOOLCHAIN ?= local
GOEXPERIMENT ?= loopvar
TESTPKGS ?= ./...
KRES_IMAGE ?= ghcr.io/siderolabs/kres:latest
CONFORMANCE_IMAGE ?= ghcr.io/siderolabs/conform:latest
Expand All @@ -37,28 +40,32 @@ PROGRESS ?= auto
PUSH ?= false
CI_ARGS ?=
COMMON_ARGS = --file=Dockerfile
COMMON_ARGS += --provenance=false
COMMON_ARGS += --progress=$(PROGRESS)
COMMON_ARGS += --platform=$(PLATFORM)
COMMON_ARGS += --push=$(PUSH)
COMMON_ARGS += --build-arg=ARTIFACTS="$(ARTIFACTS)"
COMMON_ARGS += --build-arg=SHA="$(SHA)"
COMMON_ARGS += --build-arg=TAG="$(TAG)"
COMMON_ARGS += --build-arg=ABBREV_TAG="$(ABBREV_TAG)"
COMMON_ARGS += --build-arg=USERNAME="$(USERNAME)"
COMMON_ARGS += --build-arg=REGISTRY="$(REGISTRY)"
COMMON_ARGS += --build-arg=TOOLCHAIN="$(TOOLCHAIN)"
COMMON_ARGS += --build-arg=CGO_ENABLED="$(CGO_ENABLED)"
COMMON_ARGS += --build-arg=GO_BUILDFLAGS="$(GO_BUILDFLAGS)"
COMMON_ARGS += --build-arg=GO_LDFLAGS="$(GO_LDFLAGS)"
COMMON_ARGS += --build-arg=GOLANGCILINT_VERSION="$(GOLANGCILINT_VERSION)"
COMMON_ARGS += --build-arg=GOFUMPT_VERSION="$(GOFUMPT_VERSION)"
COMMON_ARGS += --build-arg=GOIMPORTS_VERSION="$(GOIMPORTS_VERSION)"
COMMON_ARGS += --build-arg=GOTOOLCHAIN="$(GOTOOLCHAIN)"
COMMON_ARGS += --build-arg=GOEXPERIMENT="$(GOEXPERIMENT)"
COMMON_ARGS += --build-arg=PROTOBUF_GO_VERSION="$(PROTOBUF_GO_VERSION)"
COMMON_ARGS += --build-arg=GRPC_GO_VERSION="$(GRPC_GO_VERSION)"
COMMON_ARGS += --build-arg=GRPC_GATEWAY_VERSION="$(GRPC_GATEWAY_VERSION)"
COMMON_ARGS += --build-arg=VTPROTOBUF_VERSION="$(VTPROTOBUF_VERSION)"
COMMON_ARGS += --build-arg=DEEPCOPY_VERSION="$(DEEPCOPY_VERSION)"
COMMON_ARGS += --build-arg=GOLANGCILINT_VERSION="$(GOLANGCILINT_VERSION)"
COMMON_ARGS += --build-arg=GOIMPORTS_VERSION="$(GOIMPORTS_VERSION)"
COMMON_ARGS += --build-arg=GOFUMPT_VERSION="$(GOFUMPT_VERSION)"
COMMON_ARGS += --build-arg=TESTPKGS="$(TESTPKGS)"
TOOLCHAIN ?= docker.io/golang:1.20-alpine
TOOLCHAIN ?= docker.io/golang:1.21-alpine

# help menu

Expand Down Expand Up @@ -126,7 +133,8 @@ lint-gofumpt: ## Runs gofumpt linter.
.PHONY: fmt
fmt: ## Formats the source code
@docker run --rm -it -v $(PWD):/src -w /src golang:$(GO_VERSION) \
bash -c "export GO111MODULE=on; export GOPROXY=https://proxy.golang.org; \
bash -c "export GOEXPERIMENT=loopvar; export GOTOOLCHAIN=local; \
export GO111MODULE=on; export GOPROXY=https://proxy.golang.org; \
go install mvdan.cc/gofumpt@$(GOFUMPT_VERSION) && \
gofumpt -w ."

Expand All @@ -150,7 +158,7 @@ unit-tests-race: ## Performs unit tests with race detection enabled.

.PHONY: coverage
coverage: ## Upload coverage data to codecov.io.
bash -c "bash <(curl -s https://codecov.io/bash) -f $(ARTIFACTS)/coverage.txt -X fix"
bash -c "bash <(curl -s https://codecov.io/bash) -f $(ARTIFACTS)/coverage-unit-tests.txt -X fix"

.PHONY: lint-markdown
lint-markdown: ## Runs markdownlint.
Expand Down
2 changes: 2 additions & 0 deletions channel/recv.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import "context"
// RecvWithContext tries to receive a value from a channel which is aborted if the context is canceled.
//
// Function returns true if the value was received, false if the context was canceled or the channel was closed.
//
// Deprecated: use plain old select instead.
func RecvWithContext[T any](ctx context.Context, ch <-chan T) (T, bool) {
select {
case <-ctx.Done():
Expand Down
26 changes: 13 additions & 13 deletions containers/map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ package containers_test
import (
"fmt"
"math/rand"
"sync"
"testing"

"github.com/stretchr/testify/require"

"github.com/siderolabs/gen/containers"
"github.com/siderolabs/gen/xsync"
)

func TestConcurrentMap(t *testing.T) {
Expand Down Expand Up @@ -191,31 +191,31 @@ func Example_benchConcurrentMap() {
benchResult := testing.Benchmark(func(b *testing.B) {
b.ReportAllocs()

var m containers.ConcurrentMap[int, *xsync.Once[int]]
var m containers.ConcurrentMap[int, func() int]

for i := 0; i < b.N; i++ {
variable := 0

res, _ := m.GetOrCall(10, func() *xsync.Once[int] {
return &xsync.Once[int]{}
})

sink = res.Do(func() int {
variable++
res, _ := m.GetOrCall(10, func() func() int {
return sync.OnceValue(func() int {
variable++

return variable
return variable
})
})

sink = res()
}
})

if benchResult.AllocsPerOp() > 0 {
fmt.Println("this benchmark should not allocate memory")
if allocsPerOp := benchResult.AllocsPerOp(); allocsPerOp > 1 {
fmt.Printf("this benchmark should not make more than one allocation, but it makes %d allocations per operation", allocsPerOp)
}

fmt.Println("ok")
fmt.Println(sink)

// Output:
// ok

_ = sink
// 1
}
Loading

0 comments on commit 36a3ae3

Please sign in to comment.