Debian Codegraph @

packet_stringifier.go

raw symbols SCIP
1 // SPDX-FileCopyrightText: 2026 The Pion community <https://pion.ly>
2 // SPDX-License-Identifier: MIT
3
4 package rtcp
5
6 import (
7 "fmt"
8 "reflect"
9 )
10
11 /*
12 Converts an RTCP Packet into a human-readable format. The Packets
13 themselves can control the presentation as follows:
14
15 - Fields of a type that have a String() method will be formatted
16 with that String method (which should not emit '\n' characters)
17
18 - Otherwise, fields with a tag containing a "fmt" string will use that
19 format when serializing the value. For example, to format an SSRC
20 value as base 16 insted of base 10:
21
22 type ExamplePacket struct {
23 LocalSSRC uint32 `fmt:"0x%X"`
24 RemotsSSRCs []uint32 `fmt:"%X"`
25 }
26
27 - If no fmt string is present, "%+v" is used by default
28
29 The intention of this stringify() function is to simplify creation
30 of String() methods on new packet types, as it provides a simple
31 baseline implementation that works well in the majority of cases.
32 */
33 func stringify(p Packet) string {
34 value := reflect.Indirect(reflect.ValueOf(p))
35
36 return formatField(value.Type().String(), "", p, "")
37 }
38
39 //nolint:gocognit,cyclop
40 func formatField(name string, format string, f any, indent string) string {
41 out := indent
42 value := reflect.ValueOf(f)
43
44 if !value.IsValid() {
45 return fmt.Sprintf("%s%s: <nil>\n", out, name)
46 }
47
48 isPacket := reflect.TypeOf(f).Implements(reflect.TypeFor[Packet]())
49
50 // Resolve pointers to their underlying values
51 if value.Type().Kind() == reflect.Ptr && !value.IsNil() {
52 underlying := reflect.Indirect(value)
53 if underlying.IsValid() {
54 value = underlying
55 }
56 }
57
58 // If the field type has a custom String method, use that
59 // (unless we're a packet, since we want to avoid recursing
60 // back into this function if the Packet's String() method
61 // uses it)
62 if stringMethod := value.MethodByName("String"); !isPacket && stringMethod.IsValid() {
63 out += fmt.Sprintf("%s: %s\n", name, stringMethod.Call([]reflect.Value{}))
64
65 return out
66 }
67
68 switch value.Kind() {
69 case reflect.Struct:
70 out += fmt.Sprintf("%s:\n", name)
71 for i := 0; i < value.NumField(); i++ {
72 if value.Field(i).CanInterface() {
73 format = value.Type().Field(i).Tag.Get("fmt")
74 if format == "" {
75 format = "%+v"
76 }
77 out += formatField(value.Type().Field(i).Name, format, value.Field(i).Interface(), indent+"\t")
78 }
79 }
80 case reflect.Slice:
81 childKind := value.Type().Elem().Kind()
82 _, hasStringMethod := value.Type().Elem().MethodByName("String")
83 if hasStringMethod || childKind == reflect.Struct || childKind == reflect.Ptr ||
84 childKind == reflect.Interface || childKind == reflect.Slice {
85 out += fmt.Sprintf("%s:\n", name)
86 for i := 0; i < value.Len(); i++ {
87 childName := fmt.Sprint(i)
88 // Since interfaces can hold different types of things, we add the
89 // most specific type name to the name to make it clear what the
90 // subsequent fields represent.
91 if value.Index(i).Kind() == reflect.Interface {
92 childName += fmt.Sprintf(" (%s)", reflect.Indirect(reflect.ValueOf(value.Index(i).Interface())).Type())
93 }
94 if value.Index(i).CanInterface() {
95 out += formatField(childName, format, value.Index(i).Interface(), indent+"\t")
96 }
97 }
98
99 return out
100 }
101
102 // If we didn't take care of stringing the value already, we fall through to the
103 // generic case. This will print slices of basic types on a single line.
104 fallthrough
105 default:
106 if value.CanInterface() {
107 out += fmt.Sprintf("%s: "+format+"\n", name, value.Interface())
108 }
109 }
110
111 return out
112 }