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

Method Marshal

util/amf.go:210–284  ·  view source on GitHub ↗
(v any)

Source from the content-addressed store, hash-verified

208}
209
210func (amf *AMF) Marshal(v any) []byte {
211 if v == nil {
212 amf.WriteByte(AMF0_NULL)
213 return amf.Buffer
214 }
215 switch vv := v.(type) {
216 case string:
217 if l := len(vv); l > 0xFFFF {
218 amf.WriteByte(AMF0_LONG_STRING)
219 amf.WriteUint32(uint32(l))
220 } else {
221 amf.WriteByte(AMF0_STRING)
222 amf.WriteUint16(uint16(l))
223 }
224 amf.WriteString(vv)
225 case float64, uint, float32, int, int16, int32, int64, uint16, uint32, uint64, uint8, int8:
226 amf.WriteByte(AMF0_NUMBER)
227 amf.WriteFloat64(ToFloat64(vv))
228 case bool:
229 amf.WriteByte(AMF0_BOOLEAN)
230 if vv {
231 amf.WriteByte(1)
232 } else {
233 amf.WriteByte(0)
234 }
235 case EcmaArray:
236 if vv == nil {
237 amf.WriteByte(AMF0_NULL)
238 return amf.Buffer
239 }
240 amf.WriteByte(AMF0_ECMA_ARRAY)
241 amf.WriteUint32(uint32(len(vv)))
242 for k, v := range vv {
243 amf.writeProperty(k, v)
244 }
245 amf.Write(END_OBJ)
246 case map[string]any:
247 if vv == nil {
248 amf.WriteByte(AMF0_NULL)
249 return amf.Buffer
250 }
251 amf.WriteByte(AMF0_OBJECT)
252 for k, v := range vv {
253 amf.writeProperty(k, v)
254 }
255 amf.Write(END_OBJ)
256 default:
257 v := reflect.ValueOf(vv)
258 if !v.IsValid() {
259 amf.WriteByte(AMF0_NULL)
260 return amf.Buffer
261 }
262 switch v.Kind() {
263 case reflect.Slice, reflect.Array:
264 amf.WriteByte(AMF0_STRICT_ARRAY)
265 size := v.Len()
266 amf.WriteUint32(uint32(size))
267 for i := 0; i < size; i++ {

Callers 2

writePropertyMethod · 0.95
MarshalsMethod · 0.95

Calls 11

writePropertyMethod · 0.95
ToFloat64Function · 0.85
InterfaceMethod · 0.80
FieldMethod · 0.80
WriteByteMethod · 0.65
WriteUint32Method · 0.65
WriteUint16Method · 0.65
WriteStringMethod · 0.65
WriteFloat64Method · 0.65
WriteMethod · 0.65
LenMethod · 0.65

Tested by

no test coverage detected