Skip to content

Commit

Permalink
feat: support validator as a thriftgo sdk plugin (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
HeyJavaBean authored May 27, 2024
1 parent 78a2ab1 commit 36f2bc7
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 22 deletions.
16 changes: 1 addition & 15 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@ package main

import (
"flag"
"io/ioutil"
"os"

"github.com/cloudwego/thrift-gen-validator/validator"
"github.com/cloudwego/thriftgo/plugin"
)

func main() {
Expand All @@ -38,17 +36,5 @@ func main() {
os.Exit(0)
}

data, err := ioutil.ReadAll(os.Stdin)
if err != nil {
println("Failed to get input:", err.Error())
os.Exit(1)
}

req, err := plugin.UnmarshalRequest(data)
if err != nil {
println("Failed to unmarshal request:", err.Error())
os.Exit(1)
}

os.Exit(validator.Run(req))
os.Exit(validator.Run())
}
30 changes: 24 additions & 6 deletions validator/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package validator

import (
"io/ioutil"
"os"

"github.com/cloudwego/thriftgo/plugin"
Expand All @@ -28,28 +29,45 @@ const TheUseOptionMessage = "kitex_gen is not generated due to the -use option"

// Run is an entry of the plugin mode of kitex for thriftgo.
// It reads a plugin request from the standard input and writes out a response.
func Run(req *plugin.Request) int {
func Run() int {

data, err := ioutil.ReadAll(os.Stdin)
if err != nil {
println("Failed to get input:", err.Error())
os.Exit(1)
}

req, err := plugin.UnmarshalRequest(data)
if err != nil {
println("Failed to unmarshal request:", err.Error())
os.Exit(1)
}

res := HandleRequest(req)

return exit(res)
}

func HandleRequest(req *plugin.Request) *plugin.Response {
var warnings []string

g, err := newGenerator(req)
if err != nil {
return exit(&plugin.Response{Warnings: []string{err.Error()}})
return &plugin.Response{Warnings: []string{err.Error()}}
}
contents, err := g.generate()
if err != nil {
return exit(&plugin.Response{Warnings: []string{err.Error()}})
return &plugin.Response{Warnings: []string{err.Error()}}
}

warnings = append(warnings, g.warnings...)
for i := range warnings {
warnings[i] = "[thrift-gen-validator] " + warnings[i]
}
res := &plugin.Response{
return &plugin.Response{
Warnings: warnings,
Contents: contents,
}

return exit(res)
}

func exit(res *plugin.Response) int {
Expand Down
43 changes: 43 additions & 0 deletions validator/plugin_sdk.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright 2024 CloudWeGo Authors
//
// 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.

package validator

import (
"strings"

"github.com/cloudwego/thriftgo/plugin"
)

type ValidatorSDKPlugin struct {
PluginParameters []string
}

func NewValidatorSDKPlugin(params string) *ValidatorSDKPlugin {
return &ValidatorSDKPlugin{
PluginParameters: strings.Split(params, ","),
}
}

func (k *ValidatorSDKPlugin) Invoke(req *plugin.Request) (res *plugin.Response) {
return HandleRequest(req)
}

func (k *ValidatorSDKPlugin) GetName() string {
return "Validator"
}

func (k *ValidatorSDKPlugin) GetPluginParameters() []string {
return k.PluginParameters
}
2 changes: 1 addition & 1 deletion version.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ func init() {
}

const (
Version = "v0.2.3"
Version = "v0.2.4"
)

0 comments on commit 36f2bc7

Please sign in to comment.