Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add ToXXXBase64 related method #194

Merged
merged 1 commit into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions convertor/convertor.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package convertor

import (
"bytes"
"encoding/base64"
"encoding/binary"
"encoding/gob"
"encoding/json"
Expand Down Expand Up @@ -394,3 +395,87 @@ func GbkToUtf8(bs []byte) ([]byte, error) {
b, err := io.ReadAll(r)
return b, err
}

// ToStdBase64 convert data to standard base64 encoding.
func ToStdBase64(value any) string {
if value == nil || (reflect.ValueOf(value).Kind() == reflect.Ptr && reflect.ValueOf(value).IsNil()) {
return ""
}
switch value.(type) {
case []byte:
return base64.StdEncoding.EncodeToString(value.([]byte))
case string:
return base64.StdEncoding.EncodeToString([]byte(value.(string)))
case error:
return base64.StdEncoding.EncodeToString([]byte(value.(error).Error()))
default:
marshal, err := json.Marshal(value)
if err != nil {
return ""
}
return base64.StdEncoding.EncodeToString(marshal)
}
}

// ToUrlBase64 convert data to URL base64 encoding.
func ToUrlBase64(value any) string {
if value == nil || (reflect.ValueOf(value).Kind() == reflect.Ptr && reflect.ValueOf(value).IsNil()) {
return ""
}
switch value.(type) {
case []byte:
return base64.URLEncoding.EncodeToString(value.([]byte))
case string:
return base64.URLEncoding.EncodeToString([]byte(value.(string)))
case error:
return base64.URLEncoding.EncodeToString([]byte(value.(error).Error()))
default:
marshal, err := json.Marshal(value)
if err != nil {
return ""
}
return base64.URLEncoding.EncodeToString(marshal)
}
}

// ToRawStdBase64 convert data to raw standard base64 encoding.
func ToRawStdBase64(value any) string {
if value == nil || (reflect.ValueOf(value).Kind() == reflect.Ptr && reflect.ValueOf(value).IsNil()) {
return ""
}
switch value.(type) {
case []byte:
return base64.RawStdEncoding.EncodeToString(value.([]byte))
case string:
return base64.RawStdEncoding.EncodeToString([]byte(value.(string)))
case error:
return base64.RawStdEncoding.EncodeToString([]byte(value.(error).Error()))
default:
marshal, err := json.Marshal(value)
if err != nil {
return ""
}
return base64.RawStdEncoding.EncodeToString(marshal)
}
}

// ToRawUrlBase64 convert data to raw URL base64 encoding.
func ToRawUrlBase64(value any) string {
if value == nil || (reflect.ValueOf(value).Kind() == reflect.Ptr && reflect.ValueOf(value).IsNil()) {
return ""
}
switch value.(type) {
case []byte:
return base64.RawURLEncoding.EncodeToString(value.([]byte))
case string:
return base64.RawURLEncoding.EncodeToString([]byte(value.(string)))
case error:
return base64.RawURLEncoding.EncodeToString([]byte(value.(error).Error()))
default:
marshal, err := json.Marshal(value)
if err != nil {
return ""
}
return base64.RawURLEncoding.EncodeToString(marshal)
}
}
180 changes: 180 additions & 0 deletions convertor/convertor_example_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package convertor

import (
"errors"
"fmt"
"reflect"
"strconv"
Expand Down Expand Up @@ -391,3 +392,182 @@ func ExampleGbkToUtf8() {
// true
// hello
}

func ExampleToStdBase64() {
// if you want to see the result, please use 'base64.StdEncoding.DecodeString()' to decode the result

afterEncode := ToStdBase64(nil)
fmt.Println(afterEncode)

stringVal := "hello"
afterEncode = ToStdBase64(stringVal)
fmt.Println(afterEncode)

byteSliceVal := []byte("hello")
afterEncode = ToStdBase64(byteSliceVal)
fmt.Println(afterEncode)

intVal := 123
afterEncode = ToStdBase64(intVal)
fmt.Println(afterEncode)

mapVal := map[string]any{"a": "hi", "b": 2, "c": struct {
A string
B int
}{"hello", 3}}
afterEncode = ToStdBase64(mapVal)
fmt.Println(afterEncode)

floatVal := 123.456
afterEncode = ToStdBase64(floatVal)
fmt.Println(afterEncode)

boolVal := true
afterEncode = ToStdBase64(boolVal)
fmt.Println(afterEncode)

errVal := errors.New("err")
afterEncode = ToStdBase64(errVal)
fmt.Println(afterEncode)

// Output:
//
// aGVsbG8=
// aGVsbG8=
// MTIz
// eyJhIjoiaGkiLCJiIjoyLCJjIjp7IkEiOiJoZWxsbyIsIkIiOjN9fQ==
// MTIzLjQ1Ng==
// dHJ1ZQ==
// ZXJy
}

func ExampleToUrlBase64() {
// if you want to see the result, please use 'base64.URLEncoding.DecodeString()' to decode the result

stringVal := "hello"
afterEncode := ToUrlBase64(stringVal)
fmt.Println(afterEncode)

byteSliceVal := []byte("hello")
afterEncode = ToUrlBase64(byteSliceVal)
fmt.Println(afterEncode)

intVal := 123
afterEncode = ToUrlBase64(intVal)
fmt.Println(afterEncode)

mapVal := map[string]any{"a": "hi", "b": 2, "c": struct {
A string
B int
}{"hello", 3}}
afterEncode = ToUrlBase64(mapVal)
fmt.Println(afterEncode)

floatVal := 123.456
afterEncode = ToUrlBase64(floatVal)
fmt.Println(afterEncode)

boolVal := true
afterEncode = ToUrlBase64(boolVal)
fmt.Println(afterEncode)

errVal := errors.New("err")
afterEncode = ToUrlBase64(errVal)
fmt.Println(afterEncode)

// Output:
// aGVsbG8=
// aGVsbG8=
// MTIz
// eyJhIjoiaGkiLCJiIjoyLCJjIjp7IkEiOiJoZWxsbyIsIkIiOjN9fQ==
// MTIzLjQ1Ng==
// dHJ1ZQ==
// ZXJy
}

func ExampleToRawStdBase64() {
// if you want to see the result, please use 'base64.RawStdEncoding.DecodeString()' to decode the result
stringVal := "hello"
afterEncode := ToRawStdBase64(stringVal)
fmt.Println(afterEncode)

byteSliceVal := []byte("hello")
afterEncode = ToRawStdBase64(byteSliceVal)
fmt.Println(afterEncode)

intVal := 123
afterEncode = ToRawStdBase64(intVal)
fmt.Println(afterEncode)

mapVal := map[string]any{"a": "hi", "b": 2, "c": struct {
A string
B int
}{"hello", 3}}
afterEncode = ToRawStdBase64(mapVal)
fmt.Println(afterEncode)

floatVal := 123.456
afterEncode = ToRawStdBase64(floatVal)
fmt.Println(afterEncode)

boolVal := true
afterEncode = ToRawStdBase64(boolVal)
fmt.Println(afterEncode)

errVal := errors.New("err")
afterEncode = ToRawStdBase64(errVal)
fmt.Println(afterEncode)

// Output:
// aGVsbG8
// aGVsbG8
// MTIz
// eyJhIjoiaGkiLCJiIjoyLCJjIjp7IkEiOiJoZWxsbyIsIkIiOjN9fQ
// MTIzLjQ1Ng
// dHJ1ZQ
// ZXJy
}

func ExampleToRawUrlBase64() {
// if you want to see the result, please use 'base64.RawURLEncoding.DecodeString()' to decode the result

stringVal := "hello"
afterEncode := ToRawUrlBase64(stringVal)
fmt.Println(afterEncode)

byteSliceVal := []byte("hello")
afterEncode = ToRawUrlBase64(byteSliceVal)
fmt.Println(afterEncode)

intVal := 123
afterEncode = ToRawUrlBase64(intVal)
fmt.Println(afterEncode)

mapVal := map[string]any{"a": "hi", "b": 2, "c": struct {
A string
B int
}{"hello", 3}}
afterEncode = ToRawUrlBase64(mapVal)
fmt.Println(afterEncode)

floatVal := 123.456
afterEncode = ToRawUrlBase64(floatVal)
fmt.Println(afterEncode)

boolVal := true
afterEncode = ToRawUrlBase64(boolVal)
fmt.Println(afterEncode)

errVal := errors.New("err")
afterEncode = ToRawUrlBase64(errVal)
fmt.Println(afterEncode)

// Output:
// aGVsbG8
// aGVsbG8
// MTIz
// eyJhIjoiaGkiLCJiIjoyLCJjIjp7IkEiOiJoZWxsbyIsIkIiOjN9fQ
// MTIzLjQ1Ng
// dHJ1ZQ
// ZXJy
}
Loading
Loading