Skip to content

Commit 5d35b55

Browse files
committed
feat: Initial version
0 parents  commit 5d35b55

26 files changed

+3572
-0
lines changed

.editorconfig

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
root = true
2+
3+
[*]
4+
indent_style = tab
5+
end_of_line = lf
6+
charset = utf-8
7+
trim_trailing_whitespace = true
8+
insert_final_newline = true
9+
10+
[*.md]
11+
indent_style = space
12+
indent_size = 2
13+
trim_trailing_whitespace = false
14+
15+
[{*.yml,*.yaml}]
16+
indent_style = space
17+
indent_size = 2
18+
19+
[package.json]
20+
indent_style = space
21+
indent_size = 2

.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
vendor/
2+
.vscode/
3+
.idea/
4+
.DS_Store
5+
.envrc
6+
7+
# Go workspace file
8+
go.work
9+
dist/

.golangci.yml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
linters-settings:
2+
govet:
3+
check-shadowing: true
4+
misspell:
5+
locale: US
6+
7+
linters:
8+
disable-all: true
9+
enable:
10+
- bodyclose
11+
- contextcheck
12+
- depguard
13+
- dogsled
14+
- dupl
15+
- errcheck
16+
- exportloopref
17+
- exhaustive
18+
- goconst
19+
- gocyclo
20+
- gofmt
21+
- goimports
22+
- goprintffuncname
23+
- gosimple
24+
- govet
25+
- ineffassign
26+
- misspell
27+
- nakedret
28+
- noctx
29+
- nolintlint
30+
- revive
31+
- staticcheck
32+
- typecheck
33+
- unconvert
34+
- unparam
35+
- unused
36+
- whitespace

.goreleaser.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# This is an example .goreleaser.yml file with some sensible defaults.
2+
# Make sure to check the documentation at https://goreleaser.com
3+
before:
4+
hooks:
5+
# You may remove this if you don't use go modules.
6+
- go mod tidy
7+
# you may remove this if you don't need go generate
8+
- go generate ./...
9+
builds:
10+
- skip: true

.pre-commit-config.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
repos:
2+
- repo: https://github.com/alessandrojcm/commitlint-pre-commit-hook
3+
rev: v9.4.0
4+
hooks:
5+
- id: commitlint
6+
stages: [commit-msg]
7+
- repo: https://github.com/golangci/golangci-lint
8+
rev: v1.52.2
9+
hooks:
10+
- id: golangci-lint
11+
- repo: https://github.com/dnephin/pre-commit-golang
12+
rev: v0.5.1
13+
hooks:
14+
- id: go-mod-tidy

LICENSE

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Copyright 2023 Level Four AB
2+
3+
Permission is hereby granted, free of charge, to any person obtaining
4+
a copy of this software and associated documentation files (the
5+
"Software"), to deal in the Software without restriction, including
6+
without limitation the rights to use, copy, modify, merge, publish,
7+
distribute, sublicense, and/or sell copies of the Software, and to
8+
permit persons to whom the Software is furnished to do so, subject to
9+
the following conditions:
10+
11+
The above copyright notice and this permission notice shall be
12+
included in all copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# conversions-go
2+
3+
Conversions is a module to convert between types in Go, with support for
4+
`reflect.Type` or a custom type indicator, custom conversion functions and
5+
indirect conversions.
6+
7+
## Features
8+
9+
- Uses generics in `Conversions` for type safety
10+
- Supports `reflect.Type` or a custom type indicator
11+
- Built-in conversions for `reflect.Type`
12+
- Error handling, conversions can safely error if values cannot be converted
13+
- Custom conversion functions can be registered
14+
- Indirect conversions, if a direct conversion is not found between two types
15+
a chain of conversions will be searched for
16+
- Thread-safe
17+
18+
## Installation
19+
20+
```bash
21+
go get github.com/levelfourab/conversions-go
22+
```
23+
24+
## Using
25+
26+
If you want to use conversions with your own types and information you need to
27+
create a `Conversions` using the `WithTypeExtractor` option. The type extractor
28+
is in charge of taking a value and returning the type indicator for that value.
29+
30+
This type is then used during conversion. If a direct conversion is not found
31+
between the two types, a chain of conversions will be searched for.
32+
33+
Example:
34+
35+
```go
36+
type MediaType string
37+
38+
type StringWithMediaType struct {
39+
MediaType MediaType
40+
Value string
41+
}
42+
43+
conversions, err := conversions.New(
44+
conversions.WithTypeExtractor(func(v StringWithMediaType) (MediaType, error) {
45+
return v.MediaType, nil
46+
}),
47+
)
48+
49+
conversions.AddConversion("text/html", "text/plain", func(v string) (string, error) {
50+
// Using a hypothetical html2text package
51+
return html2text.Convert(v)
52+
})
53+
54+
// Perform the conversion
55+
asPlainText, err := conversions.Convert(StringWithMediaType{
56+
MediaType: "text/html",
57+
Value: "<h1>Hello World</h1>",
58+
}, "text/plain")
59+
```
60+
61+
### With Go-types
62+
63+
The `types` sub-package is available for the most common use case, converting
64+
between Go types. Part of this includes default conversions for many built-in
65+
types such as `string`, `int` (and variants), `uint` (and variants), `float32`
66+
and `float64`.
67+
68+
Example:
69+
70+
```go
71+
conversions, err := types.New(
72+
types.WithDefaultConversions(),
73+
)
74+
75+
// Convert a string to an int, using types.TypeInt for convenience
76+
asInt, err := conversions.Convert("1", types.TypeInt)
77+
78+
// Or using reflect.Type directly
79+
asInt, err := conversions.Convert("1", reflect.TypeOf(int(0)))
80+
```
81+
82+
Aliases are available to not have to carry generic types around:
83+
84+
- `types.Conversions` is an alias for `conversions.Conversions[reflect.Type, any]`
85+
- `types.Converter` is an alias for `conversions.Converter[any]`
86+
- `types.Option` is an alias for `conversions.Option[reflect.Type, any]`
87+
88+
## License
89+
90+
conversions-go is licensed under the MIT License. See [LICENSE](LICENSE) for
91+
the full license text.

commitlint.config.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
module.exports = {
2+
rules: {
3+
'body-leading-blank': [ 1, 'always' ],
4+
'footer-leading-blank': [ 1, 'always' ],
5+
'scope-case': [ 2, 'always', 'lower-case' ],
6+
'subject-case': [
7+
2,
8+
'never',
9+
[ 'lower-case' ]
10+
],
11+
'subject-empty': [ 2, 'never' ],
12+
'subject-full-stop': [ 2, 'never', '.' ],
13+
'type-case': [ 2, 'always', 'lower-case' ],
14+
'type-empty': [ 2, 'never' ],
15+
'type-enum': [
16+
2,
17+
'always',
18+
[
19+
'build',
20+
'chore',
21+
'docs',
22+
'feat',
23+
'fix',
24+
'refactor',
25+
'revert',
26+
'style',
27+
'test'
28+
]
29+
],
30+
'scope-enum': [
31+
2,
32+
'always',
33+
[
34+
]
35+
]
36+
}
37+
};

0 commit comments

Comments
 (0)