packet_stringifier.go 3.3 KB

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