gen2.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Copyright 2012 Google, Inc. All rights reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style license
  4. // that can be found in the LICENSE file in the root of the source
  5. // tree.
  6. // +build ignore
  7. // This binary handles creating string constants and function templates for enums.
  8. //
  9. // go run gen.go | gofmt > enums_generated.go
  10. package main
  11. import (
  12. "fmt"
  13. "log"
  14. "os"
  15. "text/template"
  16. "time"
  17. )
  18. const fmtString = `// Copyright 2012 Google, Inc. All rights reserved.
  19. package layers
  20. // Created by gen2.go, don't edit manually
  21. // Generated at %s
  22. import (
  23. "fmt"
  24. "github.com/google/gopacket"
  25. )
  26. `
  27. var funcsTmpl = template.Must(template.New("foo").Parse(`
  28. // Decoder calls {{.Name}}Metadata.DecodeWith's decoder.
  29. func (a {{.Name}}) Decode(data []byte, p gopacket.PacketBuilder) error {
  30. return {{.Name}}Metadata[a].DecodeWith.Decode(data, p)
  31. }
  32. // String returns {{.Name}}Metadata.Name.
  33. func (a {{.Name}}) String() string {
  34. return {{.Name}}Metadata[a].Name
  35. }
  36. // LayerType returns {{.Name}}Metadata.LayerType.
  37. func (a {{.Name}}) LayerType() gopacket.LayerType {
  38. return {{.Name}}Metadata[a].LayerType
  39. }
  40. type errorDecoderFor{{.Name}} int
  41. func (a *errorDecoderFor{{.Name}}) Decode(data []byte, p gopacket.PacketBuilder) error {
  42. return a
  43. }
  44. func (a *errorDecoderFor{{.Name}}) Error() string {
  45. return fmt.Sprintf("Unable to decode {{.Name}} %d", int(*a))
  46. }
  47. var errorDecodersFor{{.Name}} [{{.Num}}]errorDecoderFor{{.Name}}
  48. var {{.Name}}Metadata [{{.Num}}]EnumMetadata
  49. func initUnknownTypesFor{{.Name}}() {
  50. for i := 0; i < {{.Num}}; i++ {
  51. errorDecodersFor{{.Name}}[i] = errorDecoderFor{{.Name}}(i)
  52. {{.Name}}Metadata[i] = EnumMetadata{
  53. DecodeWith: &errorDecodersFor{{.Name}}[i],
  54. Name: "Unknown{{.Name}}",
  55. }
  56. }
  57. }
  58. `))
  59. func main() {
  60. fmt.Fprintf(os.Stderr, "Writing results to stdout\n")
  61. fmt.Printf(fmtString, time.Now())
  62. types := []struct {
  63. Name string
  64. Num int
  65. }{
  66. {"LinkType", 256},
  67. {"EthernetType", 65536},
  68. {"PPPType", 65536},
  69. {"IPProtocol", 256},
  70. {"SCTPChunkType", 256},
  71. {"PPPoECode", 256},
  72. {"FDDIFrameControl", 256},
  73. {"EAPOLType", 256},
  74. {"ProtocolFamily", 256},
  75. {"Dot11Type", 256},
  76. {"USBTransportType", 256},
  77. }
  78. fmt.Println("func init() {")
  79. for _, t := range types {
  80. fmt.Printf("initUnknownTypesFor%s()\n", t.Name)
  81. }
  82. fmt.Println("initActualTypeData()")
  83. fmt.Println("}")
  84. for _, t := range types {
  85. if err := funcsTmpl.Execute(os.Stdout, t); err != nil {
  86. log.Fatalf("Failed to execute template %s: %v", t.Name, err)
  87. }
  88. }
  89. }