MCPcopy Create free account
hub / github.com/Monibuca/engine / Marshal

Method Marshal

util/amf3.go:175–289  ·  view source on GitHub ↗
(v any)

Source from the content-addressed store, hash-verified

173}
174
175func (amf *AMF3) Marshal(v any) []byte {
176 if v == nil {
177 amf.WriteByte(AMF3_NULL)
178 return amf.Buffer
179 }
180 switch vv := v.(type) {
181 case string:
182 amf.WriteByte(AMF3_STRING)
183 amf.writeString(vv)
184 case bool:
185 if vv {
186 amf.WriteByte(AMF3_TRUE)
187 } else {
188 amf.WriteByte(AMF3_FALSE)
189 }
190 case int, int8, int16, int32, int64:
191 var value int64
192 reflect.ValueOf(&value).Elem().Set(reflect.ValueOf(vv).Convert(reflect.TypeOf(value)))
193 if value < -0xfffffff {
194 if value > -0x7fffffff {
195 return amf.Marshal(float64(value))
196 }
197 return amf.Marshal(strconv.FormatInt(value, 10))
198 }
199 amf.WriteByte(AMF3_INTEGER)
200 amf.writeU29(uint32(value))
201 case uint, uint8, uint16, uint32, uint64:
202 var value uint64
203 reflect.ValueOf(&value).Elem().Set(reflect.ValueOf(vv).Convert(reflect.TypeOf(value)))
204 if value >= 0x20000000 {
205 if value <= 0xffffffff {
206 return amf.Marshal(float64(value))
207 }
208 return amf.Marshal(strconv.FormatUint(value, 10))
209 }
210 amf.WriteByte(AMF3_INTEGER)
211 amf.writeU29(uint32(value))
212 case float32:
213 amf.Marshal(float64(vv))
214 case float64:
215 amf.WriteByte(AMF3_DOUBLE)
216 amf.WriteFloat64(vv)
217 case map[string]any:
218 amf.WriteByte(AMF3_OBJECT)
219 index, ok := amf.ocEnc[reflect.ValueOf(vv).Pointer()]
220 if ok {
221 index <<= 1
222 amf.writeU29(uint32(index << 1))
223 return nil
224 }
225 amf.WriteByte(0x0b)
226 err := amf.writeString("")
227 if err != nil {
228 return nil
229 }
230 for k, v := range vv {
231 err = amf.writeString(k)
232 if err != nil {

Callers 1

MarshalsMethod · 0.95

Calls 9

writeStringMethod · 0.95
writeU29Method · 0.95
getFieldNameMethod · 0.95
SetMethod · 0.80
FieldMethod · 0.80
AddrMethod · 0.80
InterfaceMethod · 0.80
WriteByteMethod · 0.65
WriteFloat64Method · 0.65

Tested by

no test coverage detected