From ff6449a89649cab5f39ec541f71fb3c960802e0f Mon Sep 17 00:00:00 2001 From: Maxim Babichev Date: Sun, 7 Apr 2024 15:12:01 +0300 Subject: [PATCH] first version --- .github/dependabot.yml | 12 +++++++++ .github/workflows/changelog.yml | 14 ++++++++++ .github/workflows/golangci-lint.yml | 26 +++++++++++++++++++ .github/workflows/release.yaml | 11 ++++++++ .github/workflows/unit.yml | 28 ++++++++++++++++++++ .gitignore | 24 +++++++++++++++++ .golangci.yml | 40 +++++++++++++++++++++++++++++ LICENSE | 21 +++++++++++++++ README.md | 26 +++++++++++++++++++ flag.go | 23 +++++++++++++++++ flag_test.go | 28 ++++++++++++++++++++ go.mod | 11 ++++++++ go.sum | 10 ++++++++ 13 files changed, 274 insertions(+) create mode 100644 .github/dependabot.yml create mode 100644 .github/workflows/changelog.yml create mode 100644 .github/workflows/golangci-lint.yml create mode 100644 .github/workflows/release.yaml create mode 100644 .github/workflows/unit.yml create mode 100644 .gitignore create mode 100644 .golangci.yml create mode 100644 LICENSE create mode 100644 README.md create mode 100644 flag.go create mode 100644 flag_test.go create mode 100644 go.mod create mode 100644 go.sum diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..e9c7a2e --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,12 @@ +version: 2 +updates: + - package-ecosystem: gomod + directory: / + schedule: + interval: daily + open-pull-requests-limit: 10 + - package-ecosystem: github-actions + directory: "/" + schedule: + interval: daily + open-pull-requests-limit: 10 diff --git a/.github/workflows/changelog.yml b/.github/workflows/changelog.yml new file mode 100644 index 0000000..e1723e8 --- /dev/null +++ b/.github/workflows/changelog.yml @@ -0,0 +1,14 @@ +name: update changelog + +on: + release: + types: [released] + +permissions: {} + +jobs: + changelog: + permissions: + contents: write + secrets: inherit + uses: bavix/.github/.github/workflows/changelog.yml@0.2.5 diff --git a/.github/workflows/golangci-lint.yml b/.github/workflows/golangci-lint.yml new file mode 100644 index 0000000..2e96f44 --- /dev/null +++ b/.github/workflows/golangci-lint.yml @@ -0,0 +1,26 @@ +name: golangci-lint +on: + push: + branches: + - master + pull_request: +permissions: + contents: read +jobs: + golangci: + name: lint + runs-on: ubuntu-latest + strategy: + matrix: + go-version: [ '1.22' ] + steps: + - uses: actions/checkout@v4 + - name: Setup Go + uses: actions/setup-go@v5 + with: + go-version: ${{ matrix.go-version }} + cache: true + - name: golangci-lint + uses: golangci/golangci-lint-action@v4 + with: + version: v1.57.2 diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml new file mode 100644 index 0000000..7e3ac74 --- /dev/null +++ b/.github/workflows/release.yaml @@ -0,0 +1,11 @@ +name: go release + +on: + release: + types: [created] + +jobs: + build: + permissions: + contents: write + uses: bavix/.github/.github/workflows/go-release-binary.yml@0.2.5 diff --git a/.github/workflows/unit.yml b/.github/workflows/unit.yml new file mode 100644 index 0000000..f5c1e3a --- /dev/null +++ b/.github/workflows/unit.yml @@ -0,0 +1,28 @@ +name: Unit +on: + pull_request: + branches: + - master + +jobs: + test: + runs-on: ubuntu-latest + strategy: + matrix: + go-version: [ '1.22' ] + steps: + - uses: actions/checkout@v4 + - name: Setup Go + uses: actions/setup-go@v5 + with: + go-version: ${{ matrix.go-version }} + cache: true + - name: Install dependencies + run: go get . + - name: Test with Go + run: go test -json ./... 2>&1 | tee -a TestResults-${{ matrix.go-version }}.json + - name: Upload Go test results + uses: actions/upload-artifact@v4 + with: + name: Go-results-${{ matrix.go-version }} + path: TestResults-${{ matrix.go-version }}.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..45cb129 --- /dev/null +++ b/.gitignore @@ -0,0 +1,24 @@ +# If you prefer the allow list template instead of the deny list, see community template: +# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore +# +# Binaries for programs and plugins +*.exe +*.exe~ +*.dll +*.so +*.dylib + +# Test binary, built with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +# Dependency directories (remove the comment below to include it) +# vendor/ + +# Go workspace file +go.work + +.idea/ +vendor/ diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 0000000..c9055ec --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,40 @@ +run: + timeout: 1m +linters: + enable-all: true + disable: + # deprecated + - nosnakecase + - structcheck + - interfacer + - deadcode + - exhaustivestruct + - maligned + - ifshort + - varcheck + - golint + - scopelint + # not relevant + - varnamelen + - wrapcheck + - paralleltest + - exhaustruct +linters-settings: + lll: + line-length: 160 + gci: + sections: + - Standard + - Default + - Prefix(github.com/bavix) + depguard: + rules: + main: + allow: + - $gostd + - github.com +issues: + exclude-rules: + - path: (.+)_test.go + linters: + - dupl diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..c63de81 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2024 bavix + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..617af6c --- /dev/null +++ b/README.md @@ -0,0 +1,26 @@ +# features + +Simple implementation of the flags feature. + +### Usage + +```golang +import "github.com/bavix/features" + +const ( + Dishing features.Flag = iota + Washing + Cooking + Driving +) +//... +toggles := features.New(Dishing, Washing, Cooking) + +toggles.Has(Cooking) // true +toggles.Has(Driving) // false +``` + +--- +Supported by + +[![Supported by JetBrains](https://cdn.rawgit.com/bavix/development-through/46475b4b/jetbrains.svg)](https://www.jetbrains.com/) diff --git a/flag.go b/flag.go new file mode 100644 index 0000000..cadaada --- /dev/null +++ b/flag.go @@ -0,0 +1,23 @@ +package features + +type Flag uint64 +type Toggles uint64 + +func New(flags ...Flag) Toggles { + var r Toggles + for _, m := range flags { + r |= Toggles(1 << m) + } + + return r +} + +func (u Toggles) Has(flags ...Flag) bool { + for _, m := range flags { + if u&Toggles(1<