MCPcopy Create free account
hub / github.com/bytebase/bytebase / convertValueValueToBytes

Function convertValueValueToBytes

backend/component/export/csv.go:100–154  ·  view source on GitHub ↗
(value *structpb.Value)

Source from the content-addressed store, hash-verified

98}
99
100func convertValueValueToBytes(value *structpb.Value) []byte {
101 if value == nil || value.Kind == nil {
102 return []byte("")
103 }
104 switch value.Kind.(type) {
105 case *structpb.Value_NullValue:
106 return []byte("")
107 case *structpb.Value_StringValue:
108 var result []byte
109 result = append(result, '"')
110 result = append(result, []byte(value.GetStringValue())...)
111 result = append(result, '"')
112 return result
113 case *structpb.Value_NumberValue:
114 return []byte(strconv.FormatFloat(value.GetNumberValue(), 'f', -1, 64))
115 case *structpb.Value_BoolValue:
116 return []byte(strconv.FormatBool(value.GetBoolValue()))
117 case *structpb.Value_ListValue:
118 var buf [][]byte
119 for _, v := range value.GetListValue().Values {
120 buf = append(buf, convertValueValueToBytes(v))
121 }
122 var result []byte
123 result = append(result, '"')
124 result = append(result, '[')
125 result = append(result, []byte(strings.Join(toStringSlice(buf), ","))...)
126 result = append(result, ']')
127 result = append(result, '"')
128 return result
129 case *structpb.Value_StructValue:
130 fields := value.GetStructValue().Fields
131 keys := make([]string, 0, len(fields))
132 for k := range fields {
133 keys = append(keys, k)
134 }
135 slices.Sort(keys)
136 first := true
137 var buf []byte
138 buf = append(buf, '"')
139 for _, k := range keys {
140 if first {
141 first = false
142 } else {
143 buf = append(buf, ',')
144 }
145 buf = append(buf, []byte(k)...)
146 buf = append(buf, ':')
147 buf = append(buf, convertValueValueToBytes(fields[k])...)
148 }
149 buf = append(buf, '"')
150 return buf
151 default:
152 return []byte("")
153 }
154}
155
156func toStringSlice(bufs [][]byte) []string {
157 result := make([]string, len(bufs))

Callers 2

convertValueToBytesInSQLFunction · 0.85
convertValueToBytesInCSVFunction · 0.85

Calls 4

toStringSliceFunction · 0.85
GetStringValueMethod · 0.80
GetBoolValueMethod · 0.80
JoinMethod · 0.80

Tested by

no test coverage detected