github.com/cloudwego/dynamicgo@v0.2.6-0.20240519101509-707f41b6b834/internal/util/strings.go (about) 1 /** 2 * Copyright 2023 CloudWeGo Authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package util 18 19 import ( 20 "strings" 21 22 "github.com/cloudwego/dynamicgo/meta" 23 "github.com/fatih/structtag" 24 "github.com/iancoleman/strcase" 25 ) 26 27 func ConvertNameCase(opt meta.NameCase, name string) string { 28 switch opt { 29 case meta.CaseUpperCamel: 30 return ToUpperCamelCase(name) 31 case meta.CaseSnake: 32 return ToSnakeCase(name) 33 case meta.CaseLowerCamel: 34 return ToLowerCamelCase(name) 35 default: 36 return name 37 } 38 } 39 40 // ToSnakeCase converts a camelCase string to snake_case. 41 func ToSnakeCase(name string) string { 42 return strcase.ToSnake(name) 43 } 44 45 // ToCamelCase converts a snake_case string to camelCase. 46 func ToUpperCamelCase(name string) string { 47 return strcase.ToCamel(name) 48 } 49 50 // LowerFirstLetter 51 func ToLowerCamelCase(name string) string { 52 return strcase.ToLowerCamel(name) 53 } 54 55 func SplitTagOptions(tag string) (ret []string, err error) { 56 tags, err := structtag.Parse(tag) 57 if err != nil { 58 // try replace `\"` 59 tag = strings.Replace(tag, `\"`, `"`, -1) 60 tags, err = structtag.Parse(tag) 61 if err != nil { 62 return nil, err 63 } 64 } 65 // kv := strings.Trim(tag, "\n\b\t\r") 66 // i := strings.Index(kv, ":") 67 // if i <= 0 { 68 // return nil, fmt.Errorf("invalid go tag: %s", tag) 69 // } 70 // ret = append(ret, kv[:i]) 71 // val := strings.Trim(kv[i+1:], "\n\b\t\r") 72 73 // v, err := strconv.Unquote(kv[i+1:]) 74 // if err != nil { 75 // return ret, err 76 // } 77 // vs := strings.Split(v, ",") 78 // ret = append(ret, vs...) 79 for _, t := range tags.Tags() { 80 ret = append(ret, t.Key) 81 ret = append(ret, t.Name) 82 } 83 return ret, nil 84 } 85 86 func SplitGoTags(input string) []string { 87 out := make([]string, 0, 4) 88 ns := len(input) 89 90 flag := false 91 prev := 0 92 i := 0 93 for i = 0; i < ns; i++ { 94 c := input[i] 95 if c == '"' { 96 flag = !flag 97 } 98 if !flag && c == ' ' { 99 if prev < i { 100 out = append(out, input[prev:i]) 101 } 102 prev = i + 1 103 } 104 } 105 if i != 0 && prev < i { 106 out = append(out, input[prev:i]) 107 } 108 109 return out 110 } 111 112 func SplitPrefix(t string) (pkg, name string) { 113 idx := strings.Index(t, ".") 114 if idx == -1 { 115 return "", t 116 } 117 return t[:idx], t[idx+1:] 118 } 119 120 func SplitSubfix(t string) (typ, val string) { 121 idx := strings.LastIndex(t, ".") 122 if idx == -1 { 123 return "", t 124 } 125 return t[:idx], t[idx+1:] 126 } 127 128 func SplitSubfix2(t string) (typ, val string) { 129 idx := strings.IndexAny(t, ".") 130 if idx == -1 { 131 return "", t 132 } 133 return t[:idx], t[idx+1:] 134 }