-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
235 changed files
with
4,192 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.hugo_build.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
title: "{{ replace .Name "-" " " | title }}" | ||
date: {{ .Date }} | ||
draft: true | ||
--- | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
baseURL = "https://cvt.shockerli.net" | ||
title = "cvt" | ||
theme = "hugo-geekdoc" | ||
|
||
defaultContentLanguage = 'en' | ||
|
||
pluralizeListTitles = false | ||
|
||
# Geekdoc required configuration | ||
pygmentsUseClasses = true | ||
pygmentsCodeFences = true | ||
disablePathToLower = true | ||
|
||
# Required if you want to render robots.txt template | ||
enableRobotsTXT = true | ||
|
||
# Needed for mermaid shortcodes | ||
[markup] | ||
[markup.goldmark.renderer] | ||
# Needed for mermaid shortcode | ||
unsafe = true | ||
[markup.tableOfContents] | ||
startLevel = 1 | ||
endLevel = 9 | ||
|
||
[taxonomies] | ||
tag = "tags" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
--- | ||
en: | ||
languageName: "English" | ||
contentDir: "content/en" | ||
weight: 10 | ||
|
||
zh-cn: | ||
languageName: "简体中文" | ||
contentDir: "content/zh-cn" | ||
weight: 20 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
--- | ||
title: 'Go type conversion' | ||
geekdocNav: false | ||
geekdocAlign: center | ||
geekdocAnchor: false | ||
--- | ||
|
||
<!-- markdownlint-capture --> | ||
<!-- markdownlint-disable MD033 --> | ||
|
||
<span class="badge-placeholder">[![PkgGoDev](https://pkg.go.dev/badge/github.com/shockerli/cvt)](https://pkg.go.dev/github.com/shockerli/cvt)</span> | ||
<span class="badge-placeholder">[![Go Report Card](https://goreportcard.com/badge/github.com/shockerli/cvt)](https://goreportcard.com/report/github.com/shockerli/cvt)</span> | ||
<span class="badge-placeholder">[![Build Status](https://travis-ci.com/shockerli/cvt.svg?branch=master)](https://travis-ci.com/shockerli/cvt)</span> | ||
<span class="badge-placeholder">[![codecov](https://codecov.io/gh/shockerli/cvt/branch/master/graph/badge.svg)](https://codecov.io/gh/shockerli/cvt)</span> | ||
<span class="badge-placeholder">![GitHub](https://img.shields.io/github/license/shockerli/cvt)</span> | ||
<span class="badge-placeholder">[![Mentioned in Awesome Go](https://awesome.re/mentioned-badge.svg)](https://github.com/avelino/awesome-go)</span> | ||
|
||
<!-- markdownlint-restore --> | ||
|
||
Simple, safe conversion of any type, including indirect/custom types. | ||
|
||
{{< button size="large" relref="usage/getting-started" >}}Getting Started{{< /button >}} | ||
|
||
## Feature overview | ||
|
||
{{< columns >}} | ||
|
||
### Safety | ||
|
||
Support to any data type, no panic, safe with application. | ||
|
||
<---> | ||
|
||
### Lightweight | ||
|
||
Less code, zero depend, the application almost no heavy burden. | ||
|
||
<---> | ||
|
||
### Easily | ||
|
||
Semantic clarity, naming, documentation, detailed, directly get started. | ||
|
||
{{< /columns >}} | ||
|
||
## Examples | ||
|
||
{{< tabs "index-example" >}} | ||
{{< tab "WITH ERROR" >}} | ||
```go | ||
cvt.IntE("12") // 12, nil | ||
cvt.Float64E("12.34") // 12.34, nil | ||
cvt.StringE(12.34) // "12.34", nil | ||
cvt.BoolE("false") // false, nil | ||
``` | ||
{{< /tab >}} | ||
|
||
{{< tab "IGNORE ERROR" >}} | ||
```go | ||
cvt.Int("12") // 12(success) | ||
cvt.Int(struct{}{}) // 0(failed) | ||
``` | ||
{{< /tab >}} | ||
|
||
{{< tab "WITH DEFAULT" >}} | ||
```go | ||
cvt.Int(struct{}{}, 12) // 12 | ||
cvt.Float("hello", 12.34) // 12.34 | ||
``` | ||
{{< /tab >}} | ||
|
||
{{< tab "CUSTOM TYPE" >}} | ||
```go | ||
type Name string | ||
|
||
var name Name = "jioby" | ||
|
||
cvt.StringE(name) // jioby, nil | ||
``` | ||
{{< /tab >}} | ||
|
||
{{< tab "INDIRECT" >}} | ||
```go | ||
var name = "jioby" | ||
|
||
cvt.StringE(&name) // jioby, nil | ||
``` | ||
{{< /tab >}} | ||
|
||
{{< tab "POINTER" >}} | ||
```go | ||
cvt.BoolP("true") // (*bool)(0x14000126180)(true) | ||
``` | ||
{{< /tab >}} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
--- | ||
title: Test in 32bit | ||
weight: 10 | ||
--- | ||
|
||
|
||
Use `i386/golang:alpine` image for testing 32bit system, detail in `Dockerfile.32bit` | ||
|
||
|
||
|
||
- build image | ||
|
||
```shell | ||
docker build -t cvt-test -f Dockerfile.32bit . | ||
``` | ||
|
||
- run container | ||
|
||
```shell | ||
docker run -it --name cvt-test -v "$PWD":/usr/src/myapp -w /usr/src/myapp cvt-test bash | ||
``` | ||
|
||
- run test | ||
|
||
```shell | ||
go test ./... | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
--- | ||
title: Development | ||
weight: 30 | ||
--- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
--- | ||
title: Type | ||
weight: 20 | ||
--- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
--- | ||
title: Bool | ||
weight: 10 | ||
--- | ||
|
||
{{< toc >}} | ||
|
||
|
||
|
||
## Bool | ||
Convert an interface to a bool type, with default value. | ||
|
||
```go | ||
cvt.Bool(0) // false | ||
cvt.Bool(nil) // false | ||
cvt.Bool("0") // false | ||
cvt.Bool("false") // false | ||
cvt.Bool([]int{}) // false | ||
|
||
cvt.Bool(true) // true | ||
cvt.Bool("true") // true | ||
cvt.Bool([]int{1, 2}) // true | ||
cvt.Bool([]byte("true")) // true | ||
``` | ||
|
||
## BoolE | ||
Convert an interface to a bool type. | ||
|
||
```go | ||
cvt.BoolE(0) // false,nil | ||
cvt.BoolE(nil) // false,nil | ||
cvt.BoolE("0") // false,nil | ||
cvt.BoolE("false") // false,nil | ||
cvt.BoolE([]int{}) // false,nil | ||
|
||
cvt.BoolE(true) // true,nil | ||
cvt.BoolE("true") // true,nil | ||
cvt.BoolE([]int{1, 2}) // true,nil | ||
cvt.BoolE([]byte("true")) // true,nil | ||
``` | ||
|
||
## BoolP | ||
Convert and store in a new bool value, and returns a pointer to it. | ||
|
||
```go | ||
cvt.BoolP("true") // (*bool)(0x14000126180)(true) | ||
``` | ||
|
||
## More Examples | ||
More case see unit: `bool_test.go` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
--- | ||
title: Float | ||
weight: 30 | ||
--- | ||
|
||
|
||
{{< toc >}} | ||
|
||
|
||
## Function | ||
- Float32 | ||
- Float32E | ||
- Float32P | ||
- Float64 | ||
- Float64E | ||
- Float64P | ||
|
||
|
||
## Examples | ||
```go | ||
cvt.Float64(int32(8)) // 8 | ||
cvt.Float64(float32(8.31)) // 8.31 | ||
cvt.Float64("-8") // 8 | ||
cvt.Float64("-8.01") // 8.01 | ||
cvt.Float64(nil) // 0 | ||
cvt.Float64(true) // 1 | ||
cvt.Float64(false) // 0 | ||
|
||
type AliasTypeInt int | ||
type PointerTypeInt *AliasTypeInt | ||
cvt.Float64(AliasTypeInt(8)) // 8 | ||
cvt.Float64((*AliasTypeInt)(nil)) // 0 | ||
cvt.FLoat64((*PointerTypeInt)(nil)) // 0 | ||
|
||
cvt.Float64P("12.3") // (*float64)(0x14000126180)(12.3) | ||
``` | ||
|
||
> More case see unit: `float_test.go` | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
--- | ||
title: Int | ||
weight: 20 | ||
--- | ||
|
||
|
||
{{< toc >}} | ||
|
||
|
||
|
||
## Function | ||
- Int | ||
- IntE | ||
- IntP | ||
- Int8 | ||
- Int8E | ||
- Int8P | ||
- Int16 | ||
- Int16E | ||
- Int16P | ||
- Int32 | ||
- Int32E | ||
- Int32P | ||
- Int64 | ||
- Int64E | ||
- Int64P | ||
- Uint | ||
- UintE | ||
- UintP | ||
- Uint8 | ||
- Uint8E | ||
- Uint8P | ||
- Uint16 | ||
- Uint16E | ||
- Uint16P | ||
- Uint32 | ||
- Uint32E | ||
- Uint32P | ||
- Uint64 | ||
- Uint64E | ||
- Uint64P | ||
|
||
## Examples | ||
```go | ||
cvt.Int(int8(8)) // 8 | ||
cvt.Int(int32(8)) // 8 | ||
cvt.Int("-8.01") // -8 | ||
cvt.Int([]byte("8.00")) // 8 | ||
cvt.Int(nil) // 0 | ||
cvt.IntE("8a") // 0,err | ||
cvt.IntE([]int{}) // 0,err | ||
|
||
// alias type | ||
type OrderType uint8 | ||
cvt.Int(OrderType(3)) // 3 | ||
|
||
var po OrderType = 3 | ||
cvt.Int(&po) // 3 | ||
|
||
cvt.IntP("12") // (*int)(0x140000a4180)(12) | ||
``` | ||
|
||
> More case see unit: `int_test.go` | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
--- | ||
title: Map | ||
weight: 70 | ||
--- | ||
|
||
{{< toc >}} | ||
|
||
|
||
## StringMapE | ||
|
||
- Map | ||
```go | ||
// expect: map[string]interface{}{"111": "cvt", "222": 3.21} | ||
cvt.StringMapE(map[interface{}]interface{}{111: "cvt", "222": 3.21}) | ||
``` | ||
|
||
- Struct | ||
|
||
```go | ||
// expect: map[string]interface{}{"Name": "cvt", "Age": 3} | ||
cvt.StringMapE(struct { | ||
Name string | ||
Age int | ||
}{"cvt", 3}) | ||
``` | ||
|
||
- JSON | ||
```go | ||
// expect: map[string]interface{}{"name": "cvt", "age": 3.21} | ||
cvt.StringMapE(`{"name":"cvt","age":3.21}`) | ||
``` | ||
|
||
|
||
> More case see unit: `map_test.go` | ||
Oops, something went wrong.