mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-10 02:43:37 +02:00
refactor and typo fixes
This commit is contained in:
@@ -102,7 +102,7 @@ func (p BidirectionalPipe) Start() E.Error {
|
||||
// Copyright 2009 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// This is a copy of io.Copy with context handling
|
||||
// Author: yusing <yusing@6uo.me>
|
||||
// Author: yusing <yusing@6uo.me>.
|
||||
func Copy(dst *ContextWriter, src *ContextReader) (err error) {
|
||||
size := 32 * 1024
|
||||
if l, ok := src.Reader.(*io.LimitedReader); ok && int64(size) > l.N {
|
||||
|
||||
@@ -18,9 +18,10 @@ func NearestField(input string, s any) string {
|
||||
if t.Kind() == reflect.Ptr {
|
||||
t = t.Elem()
|
||||
}
|
||||
if t.Kind() == reflect.Struct {
|
||||
switch t.Kind() {
|
||||
case reflect.Struct:
|
||||
fields = make([]string, 0)
|
||||
for i := 0; i < t.NumField(); i++ {
|
||||
for i := range t.NumField() {
|
||||
jsonTag, ok := t.Field(i).Tag.Lookup("json")
|
||||
if ok {
|
||||
fields = append(fields, jsonTag)
|
||||
@@ -28,13 +29,13 @@ func NearestField(input string, s any) string {
|
||||
fields = append(fields, t.Field(i).Name)
|
||||
}
|
||||
}
|
||||
} else if t.Kind() == reflect.Map {
|
||||
case reflect.Map:
|
||||
keys := reflect.ValueOf(s).MapKeys()
|
||||
fields = make([]string, len(keys))
|
||||
for i, key := range keys {
|
||||
fields[i] = key.String()
|
||||
}
|
||||
} else {
|
||||
default:
|
||||
panic("unsupported type: " + t.String())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ var (
|
||||
ErrInvalidType = E.New("invalid type")
|
||||
ErrNilValue = E.New("nil")
|
||||
ErrUnsettable = E.New("unsettable")
|
||||
ErrUnsupportedConvertion = E.New("unsupported convertion")
|
||||
ErrUnsupportedConversion = E.New("unsupported conversion")
|
||||
ErrMapMissingColon = E.New("map missing colon")
|
||||
ErrMapTooManyColons = E.New("map too many colons")
|
||||
ErrUnknownField = E.New("unknown field")
|
||||
@@ -176,10 +176,10 @@ func Deserialize(src SerializedObject, dst any) E.Error {
|
||||
case reflect.Struct:
|
||||
mapping := make(map[string]reflect.Value)
|
||||
for _, field := range reflect.VisibleFields(dstT) {
|
||||
mapping[ToLowerNoSnake(field.Name)] = dstV.FieldByName(field.Name)
|
||||
mapping[strutils.ToLowerNoSnake(field.Name)] = dstV.FieldByName(field.Name)
|
||||
}
|
||||
for k, v := range src {
|
||||
if field, ok := mapping[ToLowerNoSnake(k)]; ok {
|
||||
if field, ok := mapping[strutils.ToLowerNoSnake(k)]; ok {
|
||||
err := Convert(reflect.ValueOf(v), field)
|
||||
if err != nil {
|
||||
errs.Add(err.Subject(k))
|
||||
@@ -199,11 +199,11 @@ func Deserialize(src SerializedObject, dst any) E.Error {
|
||||
if err != nil {
|
||||
errs.Add(err.Subject(k))
|
||||
}
|
||||
dstV.SetMapIndex(reflect.ValueOf(ToLowerNoSnake(k)), tmp)
|
||||
dstV.SetMapIndex(reflect.ValueOf(strutils.ToLowerNoSnake(k)), tmp)
|
||||
}
|
||||
return errs.Error()
|
||||
default:
|
||||
return ErrUnsupportedConvertion.Subject("deserialize to " + dstT.String())
|
||||
return ErrUnsupportedConversion.Subject("deserialize to " + dstT.String())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -250,12 +250,12 @@ func Convert(src reflect.Value, dst reflect.Value) E.Error {
|
||||
case srcT.Kind() == reflect.Map:
|
||||
obj, ok := src.Interface().(SerializedObject)
|
||||
if !ok {
|
||||
return ErrUnsupportedConvertion.Subject(dstT.String() + " to " + srcT.String())
|
||||
return ErrUnsupportedConversion.Subject(dstT.String() + " to " + srcT.String())
|
||||
}
|
||||
return Deserialize(obj, dst.Addr().Interface())
|
||||
case srcT.Kind() == reflect.Slice:
|
||||
if dstT.Kind() != reflect.Slice {
|
||||
return ErrUnsupportedConvertion.Subject(dstT.String() + " to slice")
|
||||
return ErrUnsupportedConversion.Subject(dstT.String() + " to slice")
|
||||
}
|
||||
newSlice := reflect.MakeSlice(dstT, 0, src.Len())
|
||||
i := 0
|
||||
@@ -280,7 +280,7 @@ func Convert(src reflect.Value, dst reflect.Value) E.Error {
|
||||
var ok bool
|
||||
// check if (*T).Convertor is implemented
|
||||
if converter, ok = dst.Addr().Interface().(Converter); !ok {
|
||||
return ErrUnsupportedConvertion.Subjectf("%s to %s", srcT, dstT)
|
||||
return ErrUnsupportedConversion.Subjectf("%s to %s", srcT, dstT)
|
||||
}
|
||||
|
||||
return converter.ConvertFrom(src.Interface())
|
||||
@@ -310,6 +310,7 @@ func ConvertString(src string, dst reflect.Value) (convertible bool, convErr E.E
|
||||
}
|
||||
dst.Set(reflect.ValueOf(d))
|
||||
return
|
||||
default:
|
||||
}
|
||||
// primitive types / simple types
|
||||
switch dst.Kind() {
|
||||
@@ -392,7 +393,3 @@ func DeserializeJSON(j map[string]string, target any) error {
|
||||
}
|
||||
return json.Unmarshal(data, target)
|
||||
}
|
||||
|
||||
func ToLowerNoSnake(s string) string {
|
||||
return strings.ToLower(strings.ReplaceAll(s, "_", ""))
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package strutils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -57,6 +58,10 @@ func ParseBool(s string) bool {
|
||||
}
|
||||
}
|
||||
|
||||
func PortString(port uint16) string {
|
||||
return strconv.FormatUint(uint64(port), 10)
|
||||
}
|
||||
|
||||
func DoYouMean(s string) string {
|
||||
return "Did you mean " + ansi.HighlightGreen + s + ansi.Reset + "?"
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@ package strutils
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/text/cases"
|
||||
@@ -29,8 +28,8 @@ func ExtractPort(fullURL string) (int, error) {
|
||||
return Atoi(url.Port())
|
||||
}
|
||||
|
||||
func PortString(port uint16) string {
|
||||
return strconv.FormatUint(uint64(port), 10)
|
||||
func ToLowerNoSnake(s string) string {
|
||||
return strings.ToLower(strings.ReplaceAll(s, "_", ""))
|
||||
}
|
||||
|
||||
func LevenshteinDistance(a, b string) int {
|
||||
@@ -60,7 +59,7 @@ func LevenshteinDistance(a, b string) int {
|
||||
cost = 1
|
||||
}
|
||||
|
||||
v1[j+1] = min(v1[j]+1, v0[j+1]+1, v0[j]+cost)
|
||||
v1[j+1] = min3(v1[j]+1, v0[j+1]+1, v0[j]+cost)
|
||||
}
|
||||
|
||||
for j := 0; j <= len(b); j++ {
|
||||
@@ -71,7 +70,7 @@ func LevenshteinDistance(a, b string) int {
|
||||
return v1[len(b)]
|
||||
}
|
||||
|
||||
func min(a, b, c int) int {
|
||||
func min3(a, b, c int) int {
|
||||
if a < b && a < c {
|
||||
return a
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user