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

release: 20210524 #36

Merged
merged 5 commits into from
May 24, 2021
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
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
/lang/fastrand @zhangyunhao116 @PureWhiteWu
/lang/mcache @PureWhiteWu @zhangyunhao116
/lang/syncx @PureWhiteWu @zhangyunhao116
/lang/stringx @kaka19ace

# util code owners
/util/gopool @PureWhiteWu @zhangyunhao116
Expand Down
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@

# Dependency directories (remove the comment below to include it)
# vendor/

# IDE or Editor's config
.idea/

108 changes: 108 additions & 0 deletions cloud/metainfo/http.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
// Copyright 2021 ByteDance Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package metainfo

import (
"context"
"strings"
)

// HTTP header prefixes.
const (
HTTPPrefixTransient = "rpc-transit-"
HTTPPrefixPersistent = "rpc-persist-"

lenHPT = len(HTTPPrefixTransient)
lenHPP = len(HTTPPrefixPersistent)
)

// HTTPHeaderToCGIVariable performs an CGI variable conversion.
// For example, an HTTP header key `abc-def` will result in `ABC_DEF`.
func HTTPHeaderToCGIVariable(key string) string {
return strings.ToUpper(strings.ReplaceAll(key, "-", "_"))
}

// CGIVariableToHTTPHeader converts a CGI variable into an HTTP header key.
// For example, `ABC_DEF` will be converted to `abc-def`.
func CGIVariableToHTTPHeader(key string) string {
return strings.ToLower(strings.ReplaceAll(key, "_", "-"))
}

// HTTPHeaderSetter sets a key with a value into a HTTP header.
type HTTPHeaderSetter interface {
Set(key, value string)
}

// HTTPHeaderCarrier accepts a visitor to access all key value pairs in an HTTP header.
type HTTPHeaderCarrier interface {
Visit(func(k, v string))
}

// HTTPHeader is provided to wrap an http.Header into an HTTPHeaderCarrier.
type HTTPHeader map[string][]string

// Visit implements the HTTPHeaderCarrier interface.
func (h HTTPHeader) Visit(v func(k, v string)) {
for k, vs := range h {
v(k, vs[0])
}
}

// Set sets the header entries associated with key to the single element value.
// The key will converted into lowercase as the HTTP/2 protocol requires.
func (h HTTPHeader) Set(key, value string) {
h[strings.ToLower(key)] = []string{value}
}

// FromHTTPHeader reads metainfo from a given HTTP header and sets them into the context.
// Note that this function does not call TransferForward inside.
func FromHTTPHeader(ctx context.Context, h HTTPHeaderCarrier) context.Context {
if ctx == nil || h == nil {
return ctx
}

h.Visit(func(k, v string) {
kk := strings.ToLower(k)
ln := len(kk)

if ln > lenHPT && strings.HasPrefix(kk, HTTPPrefixTransient) {
kk = HTTPHeaderToCGIVariable(kk[lenHPT:])
ctx = WithValue(ctx, kk, v)
} else if ln > lenHPP && strings.HasPrefix(kk, HTTPPrefixPersistent) {
kk = HTTPHeaderToCGIVariable(kk[lenHPP:])
ctx = WithPersistentValue(ctx, kk, v)
}
})

return ctx
}

// ToHTTPHeader writes all metainfo into the given HTTP header.
// Note that this function does not call TransferForward inside.
func ToHTTPHeader(ctx context.Context, h HTTPHeaderSetter) {
if ctx == nil || h == nil {
return
}

for k, v := range GetAllValues(ctx) {
k := HTTPPrefixTransient + CGIVariableToHTTPHeader(k)
h.Set(k, v)
}

for k, v := range GetAllPersistentValues(ctx) {
k := HTTPPrefixPersistent + CGIVariableToHTTPHeader(k)
h.Set(k, v)
}
}
101 changes: 101 additions & 0 deletions cloud/metainfo/http_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// Copyright 2021 ByteDance Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package metainfo_test

import (
"context"
"net/http"
"testing"

"github.com/bytedance/gopkg/cloud/metainfo"
)

func TestHTTPHeaderToCGIVariable(t *testing.T) {
for k, v := range map[string]string{
"a": "A",
"aBc": "ABC",
"a1z": "A1Z",
"ab-": "AB_",
"-cd": "_CD",
"abc-def": "ABC_DEF",
"Abc-def_ghi": "ABC_DEF_GHI",
} {
assert(t, metainfo.HTTPHeaderToCGIVariable(k) == v)
}
}

func TestCGIVariableToHTTPHeader(t *testing.T) {
for k, v := range map[string]string{
"a": "a",
"aBc": "abc",
"a1z": "a1z",
"AB_": "ab-",
"_CD": "-cd",
"ABC_DEF": "abc-def",
"ABC-def_GHI": "abc-def-ghi",
} {
assert(t, metainfo.CGIVariableToHTTPHeader(k) == v)
}
}

func TestFromHTTPHeader(t *testing.T) {
assert(t, metainfo.FromHTTPHeader(nil, nil) == nil)

h := make(http.Header)
c := context.Background()
c1 := metainfo.FromHTTPHeader(c, metainfo.HTTPHeader(h))
assert(t, c == c1)

h.Set("abc", "def")
h.Set(metainfo.HTTPPrefixTransient+"123", "456")
h.Set(metainfo.HTTPPrefixTransient+"abc-def", "ghi")
h.Set(metainfo.HTTPPrefixPersistent+"xyz", "000")
c1 = metainfo.FromHTTPHeader(c, metainfo.HTTPHeader(h))
assert(t, c != c1)
vs := metainfo.GetAllValues(c1)
assert(t, len(vs) == 2, vs)
assert(t, vs["ABC_DEF"] == "ghi" && vs["123"] == "456", vs)
vs = metainfo.GetAllPersistentValues(c1)
assert(t, len(vs) == 1 && vs["XYZ"] == "000")
}

func TestToHTTPHeader(t *testing.T) {
metainfo.ToHTTPHeader(nil, nil)

h := make(http.Header)
c := context.Background()
metainfo.ToHTTPHeader(c, h)
assert(t, len(h) == 0)

c = metainfo.WithValue(c, "123", "456")
c = metainfo.WithPersistentValue(c, "abc", "def")
metainfo.ToHTTPHeader(c, h)
assert(t, len(h) == 2)
assert(t, h.Get(metainfo.HTTPPrefixTransient+"123") == "456")
assert(t, h.Get(metainfo.HTTPPrefixPersistent+"abc") == "def")
}

func TestHTTPHeader(t *testing.T) {
h := make(metainfo.HTTPHeader)
h.Set("Hello", "halo")
h.Set("hello", "world")

kvs := make(map[string]string)
h.Visit(func(k, v string) {
kvs[k] = v
})
assert(t, len(kvs) == 1)
assert(t, kvs["hello"] == "world")
}
9 changes: 9 additions & 0 deletions lang/stringx/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# stringx

## Introduction
Extension/Helper of String Operation.

## Features
- Transform(Reverse, Rotate, Shuffle ...)
- Construction(Pad, Repeat...)
- Matching(IsAlpha, IsAlphanumeric, IsNumeric ...)
16 changes: 16 additions & 0 deletions lang/stringx/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2021 ByteDance Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package stringx provides extensions of string Operation.
package stringx
76 changes: 76 additions & 0 deletions lang/stringx/exmaple_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Copyright 2021 ByteDance Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package stringx

import (
"fmt"
"unicode/utf8"
)

func Example_sub() {
fmt.Printf("Sub-[0:100]=%s\n", Sub("", 0, 100))
fmt.Printf("Sub-facgbheidjk[3:9]=%s\n", Sub("facgbheidjk", 3, 9))
fmt.Printf("Sub-facgbheidjk[-50:100]=%s\n", Sub("facgbheidjk", -50, 100))
fmt.Printf("Sub-facgbheidjk[-3:length]=%s\n", Sub("facgbheidjk", -3, utf8.RuneCountInString("facgbheidjk")))
fmt.Printf("Sub-facgbheidjk[-3:-1]=%s\n", Sub("facgbheidjk", -3, -1))
fmt.Printf("Sub-zh英文hun排[2:5]=%s\n", Sub("zh英文hun排", 2, 5))
fmt.Printf("Sub-zh英文hun排[2:-1]=%s\n", Sub("zh英文hun排", 2, -1))
fmt.Printf("Sub-zh英文hun排[-100:-1]=%s\n", Sub("zh英文hun排", -100, -1))
fmt.Printf("Sub-zh英文hun排[-100:-90]=%s\n", Sub("zh英文hun排", -100, -90))
fmt.Printf("Sub-zh英文hun排[-10:-90]=%s\n", Sub("zh英文hun排", -10, -90))

// Output:
// Sub-[0:100]=
// Sub-facgbheidjk[3:9]=gbheid
// Sub-facgbheidjk[-50:100]=facgbheidjk
// Sub-facgbheidjk[-3:length]=djk
// Sub-facgbheidjk[-3:-1]=dj
// Sub-zh英文hun排[2:5]=英文h
// Sub-zh英文hun排[2:-1]=英文hun
// Sub-zh英文hun排[-100:-1]=zh英文hun
// Sub-zh英文hun排[-100:-90]=
// Sub-zh英文hun排[-10:-90]=
}

func Example_substart() {
fmt.Printf("SubStart-[0:]=%s\n", SubStart("", 0))
fmt.Printf("SubStart-[2:]=%s\n", SubStart("", 2))
fmt.Printf("SubStart-facgbheidjk[3:]=%s\n", SubStart("facgbheidjk", 3))
fmt.Printf("SubStart-facgbheidjk[-50:]=%s\n", SubStart("facgbheidjk", -50))
fmt.Printf("SubStart-facgbheidjk[-3:]=%s\n", SubStart("facgbheidjk", -3))
fmt.Printf("SubStart-zh英文hun排[3:]=%s\n", SubStart("zh英文hun排", 3))

// Output:
// SubStart-[0:]=
// SubStart-[2:]=
// SubStart-facgbheidjk[3:]=gbheidjk
// SubStart-facgbheidjk[-50:]=facgbheidjk
// SubStart-facgbheidjk[-3:]=djk
// SubStart-zh英文hun排[3:]=文hun排
}

func Example_pad() {

fmt.Printf("PadLeft=[%s]\n", PadLeftSpace("abc", 7))
fmt.Printf("PadLeft=[%s]\n", PadLeftChar("abc", 7, '-'))
fmt.Printf("PadCenter=[%s]\n", PadCenterChar("abc", 7, '-'))
fmt.Printf("PadCenter=[%s]\n", PadCenterChar("abcd", 7, '-'))

// Output:
// PadLeft=[ abc]
// PadLeft=[----abc]
// PadCenter=[--abc--]
// PadCenter=[-abcd--]
}
53 changes: 53 additions & 0 deletions lang/stringx/is.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright 2021 ByteDance Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package stringx

import (
"unicode"
)

// IsAlpha checks if the string contains only unicode letters.
func IsAlpha(s string) bool {
for _, v := range s {
if !unicode.IsLetter(v) {
return false
}
}
return true
}

// IsAlphanumeric checks if the string contains only Unicode letters or digits.
func IsAlphanumeric(s string) bool {
for _, v := range s {
if !isAlphanumeric(v) {
return false
}
}
return true
}

// IsNumeric Checks if the string contains only digits. A decimal point is not a digit and returns false.
func IsNumeric(s string) bool {
for _, v := range s {
if !unicode.IsDigit(v) {
return false
}
}
return true
}

func isAlphanumeric(v rune) bool {
return unicode.IsDigit(v) || unicode.IsLetter(v)
}
Loading