Skip to content

Commit da613f1

Browse files
committed
impl review suggestions
1 parent c92ed03 commit da613f1

File tree

1 file changed

+12
-17
lines changed

1 file changed

+12
-17
lines changed

sql/types/strings.go

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -496,12 +496,6 @@ func ConvertToBytes(ctx context.Context, v interface{}, t sql.StringType, dest [
496496
return val, nil
497497
}
498498

499-
// ConvertToCollatedString returns the given interface as a string, along with its collation. If the Type possess a
500-
// collation, then that collation is returned. If the Type does not possess a collation (such as an integer), then the
501-
// value is converted to a string and the default collation is used. If the value is already a string then no additional
502-
// conversions are made. If the value is a byte slice then a non-copying conversion is made, which means that the
503-
// original byte slice MUST NOT be modified after being passed to this function. If modifications need to be made, then
504-
// you must allocate a new byte slice and pass that new one in.
505499
// convertToLongTextString safely converts a value to string using LongText.Convert with nil checking
506500
func convertToLongTextString(ctx context.Context, val interface{}) (string, error) {
507501
converted, _, err := LongText.Convert(ctx, val)
@@ -525,6 +519,12 @@ func convertEnumToString(ctx context.Context, val interface{}, enumType sql.Enum
525519
return convertToLongTextString(ctx, val)
526520
}
527521

522+
// ConvertToCollatedString returns the given interface as a string, along with its collation. If the Type possess a
523+
// collation, then that collation is returned. If the Type does not possess a collation (such as an integer), then the
524+
// value is converted to a string and the default collation is used. If the value is already a string then no additional
525+
// conversions are made. If the value is a byte slice then a non-copying conversion is made, which means that the
526+
// original byte slice MUST NOT be modified after being passed to this function. If modifications need to be made, then
527+
// you must allocate a new byte slice and pass that new one in.
528528
func ConvertToCollatedString(ctx context.Context, val interface{}, typ sql.Type) (string, sql.CollationID, error) {
529529
var content string
530530
var collation sql.CollationID
@@ -539,13 +539,9 @@ func ConvertToCollatedString(ctx context.Context, val interface{}, typ sql.Type)
539539
content = strVal
540540
} else if byteVal, ok := val.([]byte); ok {
541541
content = encodings.BytesToString(byteVal)
542-
} else if IsEnum(typ) {
542+
} else if enumType, ok := typ.(sql.EnumType); ok {
543543
// Handle enum types in string context - return the string value, not the index
544-
if enumType, ok := typ.(sql.EnumType); ok {
545-
content, err = convertEnumToString(ctx, val, enumType)
546-
} else {
547-
content, err = convertToLongTextString(ctx, val)
548-
}
544+
content, err = convertEnumToString(ctx, val, enumType)
549545
if err != nil {
550546
return "", sql.Collation_Unspecified, err
551547
}
@@ -560,12 +556,11 @@ func ConvertToCollatedString(ctx context.Context, val interface{}, typ sql.Type)
560556
// Handle enum types in string context even without collation
561557
if IsEnum(typ) {
562558
if enumType, ok := typ.(sql.EnumType); ok {
563-
if enumVal, ok := val.(uint16); ok {
564-
if enumStr, exists := enumType.At(int(enumVal)); exists {
565-
content = enumStr
566-
return content, collation, nil
567-
}
559+
content, err = convertEnumToString(ctx, val, enumType)
560+
if err != nil {
561+
return "", sql.Collation_Unspecified, err
568562
}
563+
return content, collation, nil
569564
}
570565
}
571566
content, err = convertToLongTextString(ctx, val)

0 commit comments

Comments
 (0)