forked from google/gnostic-go-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
94 lines (82 loc) · 3.02 KB
/
main.go
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
// Copyright 2019 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// gnostic_go_generator is a sample Gnostic plugin that generates Go
// code that supports an API.
package main
import (
"encoding/json"
"errors"
"go/format"
"path/filepath"
"strings"
"github.com/golang/protobuf/proto"
plugins "github.com/googleapis/gnostic/plugins"
surface "github.com/googleapis/gnostic/surface"
)
// This is the main function for the code generation plugin.
func main() {
env, err := plugins.NewEnvironment()
env.RespondAndExitIfError(err)
packageName, err := resolvePackageName(env.Request.OutputPath)
env.RespondAndExitIfError(err)
// Use the name used to run the plugin to decide which files to generate.
var files []string
switch {
case strings.Contains(env.Invocation, "gnostic-go-client"):
files = []string{"client.go", "types.go", "constants.go"}
case strings.Contains(env.Invocation, "gnostic-go-server"):
files = []string{"server.go", "provider.go", "types.go", "constants.go"}
default:
files = []string{"client.go", "server.go", "provider.go", "types.go", "constants.go"}
}
inputDocumentType := env.Request.Models[0].TypeUrl
for _, model := range env.Request.Models {
switch model.TypeUrl {
case "surface.v1.Model":
surfaceModel := &surface.Model{}
err = proto.Unmarshal(model.Value, surfaceModel)
if err == nil {
// Customize the code surface model for Go
NewGoLanguageModel().Prepare(surfaceModel, inputDocumentType)
modelJSON, _ := json.MarshalIndent(surfaceModel, "", " ")
modelFile := &plugins.File{Name: "model.json", Data: modelJSON}
env.Response.Files = append(env.Response.Files, modelFile)
// Create the renderer.
renderer, err := NewServiceRenderer(surfaceModel)
renderer.Package = packageName
env.RespondAndExitIfError(err)
// Run the renderer to generate files and add them to the response object.
err = renderer.Render(env.Response, files)
env.RespondAndExitIfError(err)
// Return with success.
env.RespondAndExit()
}
}
}
err = errors.New("No generated code surface model is available.")
env.RespondAndExitIfError(err)
}
// resolvePackageName converts a path to a valid package name or
// error if path can't be resolved or resolves to an invalid package name.
func resolvePackageName(p string) (string, error) {
p, err := filepath.Abs(p)
if err == nil {
p = filepath.Base(p)
_, err = format.Source([]byte("package " + p))
}
if err != nil {
return "", errors.New("invalid package name " + p)
}
return p, nil
}