Recognize stat_dir_type_empty
[govpp.git] / binapigen / plugin.go
1 //  Copyright (c) 2020 Cisco and/or its affiliates.
2 //
3 //  Licensed under the Apache License, Version 2.0 (the "License");
4 //  you may not use this file except in compliance with the License.
5 //  You may obtain a copy of the License at:
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 //  Unless required by applicable law or agreed to in writing, software
10 //  distributed under the License is distributed on an "AS IS" BASIS,
11 //  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 //  See the License for the specific language governing permissions and
13 //  limitations under the License.
14
15 package binapigen
16
17 import "fmt"
18
19 type Plugin struct {
20         Name         string
21         GenerateFile func(*Generator, *File) *GenFile
22 }
23
24 var Plugins = map[string]*Plugin{}
25 var plugins []*Plugin
26
27 func RegisterPlugin(name string, genfn func(*Generator, *File) *GenFile) {
28         if name == "" {
29                 panic("plugin name empty")
30         }
31         for _, p := range plugins {
32                 if p.Name == name {
33                         panic("duplicate plugin name: " + name)
34                 }
35         }
36         plugin := &Plugin{
37                 Name:         name,
38                 GenerateFile: genfn,
39         }
40         plugins = append(plugins, plugin)
41         Plugins[name] = plugin
42 }
43
44 func RunPlugin(name string, gen *Generator, file *File) error {
45         p, ok := Plugins[name]
46         if !ok {
47                 return fmt.Errorf("plugin not found: %q", name)
48         }
49         p.GenerateFile(gen, file)
50         return nil
51 }