Skip to content

Commit

Permalink
add sqlint support for string type enums (#135)
Browse files Browse the repository at this point in the history
  • Loading branch information
abice committed Oct 31, 2022
1 parent 58e3ed8 commit e9f5c5b
Show file tree
Hide file tree
Showing 43 changed files with 1,594 additions and 218 deletions.
7 changes: 3 additions & 4 deletions .github/workflows/build_and_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ jobs:
name: run tests with go version ${{ matrix.go }}
steps:
- name: install go
uses: actions/setup-go@v2
uses: actions/setup-go@v3
with:
go-version: ${{ matrix.go }}

- name: checkout code
uses: actions/checkout@v2
uses: actions/checkout@v3

- name: Build
run: make build
Expand All @@ -43,11 +43,10 @@ jobs:
make coveralls
fi
- name: Upload coverage
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v3
with:
name: coverage
path: coverage.*

- name: Assert no changes
if: ${{ matrix.go }} > '1.16'
run: make assert-no-changes
8 changes: 4 additions & 4 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Set up Go
uses: actions/setup-go@v2
uses: actions/setup-go@v3
with:
go-version: 1.19

Expand All @@ -28,14 +28,14 @@ jobs:
${{ runner.os }}-go-
- name: Install GoReleaser
uses: goreleaser/goreleaser-action@v2
uses: goreleaser/goreleaser-action@v3
with:
install-only: true
- name: Show GoReleaser version
run: goreleaser -v

- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v2
uses: goreleaser/goreleaser-action@v3
if: success() && startsWith(github.ref, 'refs/tags/')
with:
# either 'goreleaser' (default) or 'goreleaser-pro'
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ bin/goveralls: go.sum
snapshots: snapshots_1.17
snapshots: snapshots_1.18

snapshots_%:
snapshots_%: clean
echo "##### updating snapshots for golang $* #####"
docker run -i -t -w /app -v $(shell pwd):/app --entrypoint /bin/sh golang:$* -c './update-snapshots.sh || true'

Expand Down
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,37 @@ func (x *ImageType) UnmarshalText(text []byte) error

If you find that the options given are not adequate for your use case, there is an option to add a custom template (`-t` flag) to the processing engine so that your custom code can be created!

## Now with string typed enums

```go
// ENUM(pending, running, completed, failed)
type StrState string
```

```go
const (
// StrStatePending is a StrState of type pending.
StrStatePending StrState = "pending"
// StrStateRunning is a StrState of type running.
StrStateRunning StrState = "running"
// StrStateCompleted is a StrState of type completed.
StrStateCompleted StrState = "completed"
// StrStateFailed is a StrState of type failed.
StrStateFailed StrState = "failed"
)
```

If you would like to get integer values in sql, but strings elsewhere, you can assign an int value in the declaration
like always, and specify the `--sqlint` flag. Those values will be then used to convey the int value to sql, while allowing you to use only strings elsewhere.
This might be helpful for things like swagger docs where you want the same type being used on the api layer, as you do in the
sql layer, and not have swagger assume that your enumerations are integers, but are in fact strings!

```go
// swagger:enum StrState
// ENUM(pending, running, completed, failed)
type StrState string
```

## Goal

The goal of go-enum is to create an easy to use enum generator that will take a decorated type declaration like `type EnumName int` and create the associated constant values and funcs that will make life a little easier for adding new values.
Expand Down
5 changes: 4 additions & 1 deletion _example/animal_enum.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion _example/color_enum.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions _example/commented_enum.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion _example/custom_prefix_enum.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion _example/enum_32_bit_enum.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion _example/enum_64_bit_enum.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions _example/example_enum.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion _example/force_lower_enum.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions _example/negative_enum.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion _example/replace_prefix_enum.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion _example/replace_prefix_int_enum.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit e9f5c5b

Please sign in to comment.