Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimize: optimize for thriftgo sdk #206

Merged
merged 3 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion args/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,6 @@ Available generators (and options): go
// println(align(b.Options()))
//}
println()
os.Exit(2)
}

// align the help strings for plugin options.
Expand Down
41 changes: 37 additions & 4 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"

"github.com/cloudwego/thriftgo/utils/dir_utils"

"gopkg.in/yaml.v3"
)

Expand Down Expand Up @@ -78,25 +81,45 @@ func (r *RefConfig) IsAllFieldsEmpty() bool {
}

var globalConfig *Config
var useAbs bool = true

func GetRef(name string) *RefConfig {
if globalConfig == nil {
return nil
}
return globalConfig.Ref[name]
if useAbs {
name, _ = filepath.Abs(name)
}
refConfig, ok := globalConfig.Ref[name]
if globalConfig.Debug {
if ok {
fmt.Printf("[idl-ref-get]Successfully Get: %s\n", name)
} else {
fmt.Printf("[idl-ref-get]Not IDL Ref: %s\n", name)
}
}
return refConfig
}

func init() {
func LoadConfig() error {
config, err := initConfig()
if err != nil {
panic(errors.New("failed to parse idl ref config: " + err.Error()))
return errors.New("failed to parse idl ref config: " + err.Error())
}
globalConfig = config
return nil
}

func initConfig() (*Config, error) {
configPaths := []string{"idl-ref.yml", "idl-ref.yaml"}
for _, path := range configPaths {
if dir_utils.HasGlobalWd() {
dirpath, err := dir_utils.Getwd()
if err != nil {
return nil, err
}
path = filepath.Join(dirpath, path)
}
_, err := os.Stat(path)
if err == nil {
return loadConfig(path)
Expand Down Expand Up @@ -124,7 +147,17 @@ func loadConfig(filename string) (*Config, error) {
config.Ref = map[string]*RefConfig{}
for k, v := range rawConfig.Ref {
var rc RefConfig

// if use absolute path to match idl-ref path and current file path
// convert the idl path to absoulute path
if useAbs {
k, err = dir_utils.ToAbsolute(k)
if err != nil {
return nil, err
}
}
if config.Debug {
fmt.Printf("[idl-ref-register]Path: %s\n", k)
}
switch val := v.(type) {
case map[string]interface{}:
err = mapToStruct(val, &rc)
Expand Down
6 changes: 6 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"runtime/debug"
"runtime/pprof"

"github.com/cloudwego/thriftgo/config"

"time"

"github.com/cloudwego/thriftgo/args"
Expand All @@ -39,6 +41,10 @@ var (
var debugMode bool

func init() {
err := config.LoadConfig()
if err != nil {
panic(err)
}
_ = g.RegisterBackend(new(golang.GoBackend))
// export THRIFTGO_DEBUG=1
debugMode = os.Getenv("THRIFTGO_DEBUG") == "1"
Expand Down
30 changes: 23 additions & 7 deletions sdk/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,18 @@ package sdk

import (
"fmt"
"os"

"github.com/cloudwego/thriftgo/args"
"github.com/cloudwego/thriftgo/utils/dir_utils"

"github.com/cloudwego/thriftgo/config"

targs "github.com/cloudwego/thriftgo/args"
"github.com/cloudwego/thriftgo/generator"
"github.com/cloudwego/thriftgo/generator/backend"
"github.com/cloudwego/thriftgo/generator/golang"
"github.com/cloudwego/thriftgo/parser"
"github.com/cloudwego/thriftgo/plugin"
"github.com/cloudwego/thriftgo/semantic"
"github.com/cloudwego/thriftgo/utils/dir_utils"
"github.com/cloudwego/thriftgo/version"
)

Expand All @@ -34,19 +36,34 @@ func init() {
}

var (
a args.Arguments
g generator.Generator
)

func RunThriftgoAsSDK(wd string, plugins []plugin.SDKPlugin, args ...string) error {

// this should execute at the first line!
dir_utils.SetGlobalwd(wd)

err := a.Parse(append([]string{"thriftgo"}, args...))
err := config.LoadConfig()
if err != nil {
return err
}

var a targs.Arguments

err = a.Parse(append([]string{"thriftgo"}, args...))
if err != nil {
if err.Error() == "flag: help requested" {
return nil
}
return err
}

if a.AskVersion {
println("thriftgo", version.ThriftgoVersion)
return nil
}

ast, err := parser.ParseFile(a.IDL, a.Includes, true)
if err != nil {
return err
Expand Down Expand Up @@ -80,8 +97,7 @@ func RunThriftgoAsSDK(wd string, plugins []plugin.SDKPlugin, args ...string) err
}

if len(langs) == 0 {
println("No output language(s) specified")
os.Exit(2)
return fmt.Errorf("No output language(s) specified")
}

log := backend.DummyLogFunc()
Expand Down
17 changes: 17 additions & 0 deletions utils/dir_utils/dir_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package dir_utils
import (
"os"
"path/filepath"
"strings"
)

var globalwd string
Expand All @@ -40,3 +41,19 @@ func Getwd() (string, error) {
wantedwd := globalwd
return filepath.Rel(currentwd, wantedwd)
}

func ToAbsolute(k string) (string, error) {
if !strings.HasSuffix(k, "/") {
wd, err := Getwd()
if err != nil {
return "", err
}
k = filepath.Join(wd, k)
absK, err := filepath.Abs(k)
if err != nil {
return "", err
}
k = absK
}
return k, nil
}
Loading