Skip to content

Commit b3ca38d

Browse files
authored
feat(tool): add env for using prutal to marshal (#1759)
1 parent 182237c commit b3ca38d

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

tool/internal_pkg/generator/generator.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -605,11 +605,11 @@ func (g *generator) setImports(name string, pkg *PackageInfo) {
605605
pkg.AddImports("fmt")
606606
}
607607
if m.GenArgResultStruct {
608-
if env.UseProtoc() {
609-
pkg.AddImports("proto")
610-
} else {
608+
if env.UsePrutalMarshal() {
611609
// reuse "proto" so that no need to change code templates ...
612610
pkg.AddImport("proto", "github.com/cloudwego/prutal")
611+
} else {
612+
pkg.AddImports("proto")
613613
}
614614
} else {
615615
// for method Arg and Result

tool/internal_pkg/util/env/env.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,19 @@ import (
2727
func UseProtoc() bool {
2828
v := os.Getenv("KITEX_TOOL_USE_PROTOC")
2929
if v == "" {
30-
return false // disable protoc by default
30+
return false // false by default
31+
}
32+
ok, _ := strconv.ParseBool(v)
33+
return ok
34+
}
35+
36+
func UsePrutalMarshal() bool {
37+
if !UseProtoc() {
38+
return true // if not using protoc, can only use prutal
39+
}
40+
v := os.Getenv("KITEX_TOOL_USE_PRUTAL_MARSHAL")
41+
if v == "" {
42+
return false // false by default
3143
}
3244
ok, _ := strconv.ParseBool(v)
3345
return ok

tool/internal_pkg/util/env/env_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,19 @@ func TestUseProtoc(t *testing.T) {
3737
use = UseProtoc()
3838
test.Assert(t, use == false, use)
3939
}
40+
41+
func TestUsePrutalMarshal(t *testing.T) {
42+
use := UsePrutalMarshal() // default: true
43+
test.Assert(t, use == true, use)
44+
45+
t.Setenv("KITEX_TOOL_USE_PROTOC", "0")
46+
use = UsePrutalMarshal()
47+
test.Assert(t, use == true, use)
48+
49+
t.Setenv("KITEX_TOOL_USE_PROTOC", "1")
50+
use = UsePrutalMarshal()
51+
test.Assert(t, use == false, use)
52+
t.Setenv("KITEX_TOOL_USE_PRUTAL_MARSHAL", "1")
53+
use = UsePrutalMarshal()
54+
test.Assert(t, use == true, use)
55+
}

0 commit comments

Comments
 (0)