summaryrefslogtreecommitdiff
path: root/src/mongo/gotools/src/github.com/mongodb/mongo-tools/vendor/github.com/google/gopacket/layers/gen2.go
blob: 150cad77f8f347c001b39daf90a89f81de7422ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright 2012 Google, Inc. All rights reserved.
//
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file in the root of the source
// tree.

// +build ignore

// This binary handles creating string constants and function templates for enums.
//
//  go run gen.go | gofmt > enums_generated.go
package main

import (
	"fmt"
	"log"
	"os"
	"text/template"
	"time"
)

const fmtString = `// Copyright 2012 Google, Inc. All rights reserved.

package layers

// Created by gen2.go, don't edit manually
// Generated at %s

import (
  "fmt"

  "github.com/google/gopacket"
)

`

var funcsTmpl = template.Must(template.New("foo").Parse(`
// Decoder calls {{.Name}}Metadata.DecodeWith's decoder.
func (a {{.Name}}) Decode(data []byte, p gopacket.PacketBuilder) error {
	return {{.Name}}Metadata[a].DecodeWith.Decode(data, p)
}
// String returns {{.Name}}Metadata.Name.
func (a {{.Name}}) String() string {
	return {{.Name}}Metadata[a].Name
}
// LayerType returns {{.Name}}Metadata.LayerType.
func (a {{.Name}}) LayerType() gopacket.LayerType {
	return {{.Name}}Metadata[a].LayerType
}

type errorDecoderFor{{.Name}} int
func (a *errorDecoderFor{{.Name}}) Decode(data []byte, p gopacket.PacketBuilder) error {
  return a
}
func (a *errorDecoderFor{{.Name}}) Error() string {
  return fmt.Sprintf("Unable to decode {{.Name}} %d", int(*a))
}

var errorDecodersFor{{.Name}} [{{.Num}}]errorDecoderFor{{.Name}}
var {{.Name}}Metadata [{{.Num}}]EnumMetadata

func initUnknownTypesFor{{.Name}}() {
  for i := 0; i < {{.Num}}; i++ {
    errorDecodersFor{{.Name}}[i] = errorDecoderFor{{.Name}}(i)
    {{.Name}}Metadata[i] = EnumMetadata{
      DecodeWith: &errorDecodersFor{{.Name}}[i],
      Name: "Unknown{{.Name}}",
    }
  }
}
`))

func main() {
	fmt.Fprintf(os.Stderr, "Writing results to stdout\n")
	fmt.Printf(fmtString, time.Now())
	types := []struct {
		Name string
		Num  int
	}{
		{"LinkType", 256},
		{"EthernetType", 65536},
		{"PPPType", 65536},
		{"IPProtocol", 256},
		{"SCTPChunkType", 256},
		{"PPPoECode", 256},
		{"FDDIFrameControl", 256},
		{"EAPOLType", 256},
		{"ProtocolFamily", 256},
		{"Dot11Type", 256},
		{"USBTransportType", 256},
	}

	fmt.Println("func init() {")
	for _, t := range types {
		fmt.Printf("initUnknownTypesFor%s()\n", t.Name)
	}
	fmt.Println("initActualTypeData()")
	fmt.Println("}")
	for _, t := range types {
		if err := funcsTmpl.Execute(os.Stdout, t); err != nil {
			log.Fatalf("Failed to execute template %s: %v", t.Name, err)
		}
	}
}