Skip to content

Commit 8628ac6

Browse files
committed
change symMap field of llcppg.cfg to map type
1 parent 716795e commit 8628ac6

File tree

10 files changed

+65
-23
lines changed

10 files changed

+65
-23
lines changed

_xtool/llcppsymg/_cmptest/config_test/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ func TestGenDylibPaths() {
118118
tempDir := os.TempDir()
119119
tempDefaultPath, err := os.MkdirTemp(tempDir, "symblib")
120120
if err != nil {
121-
fmt.Printf(err)
121+
fmt.Println(err)
122122
return
123123
}
124124
affix := ".dylib"

_xtool/llcppsymg/_cmptest/symg_test/cjson/llcppg.cfg

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
],
77
"trimPrefixes": ["cJSON_"],
88
"cplusplus": false,
9-
"symMap": [
10-
"cJSON_AddArrayToObject:.AddArrayToObj",
11-
"cJSON_AddBoolToObject:AddBoolToObj"
12-
]
9+
"symMap": {
10+
"cJSON_AddArrayToObject":".AddArrayToObj",
11+
"cJSON_AddBoolToObject":"AddBoolToObj"
12+
}
1313
}

_xtool/llcppsymg/config/config.go

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,13 @@ func GetConf(data []byte) (Conf, error) {
3333
TrimPrefixes: GetStringArrayItem(parsedConf, "trimPrefixes"),
3434
Cplusplus: GetBoolItem(parsedConf, "cplusplus"),
3535
Mix: GetBoolItem(parsedConf, "mix"),
36-
SymMap: GetStringArrayItem(parsedConf, "symMap"),
36+
SymMap: GetMapItem[string](parsedConf, "symMap"),
3737
}
3838

39+
//cannot use GetMapItem[string](parsedConf, "symMap")
40+
// (value of type MapType[string]) as []string value
41+
// in struct literal (compile)
42+
3943
return Conf{
4044
JSON: parsedConf,
4145
Config: config,
@@ -55,6 +59,50 @@ func GetStringItem(obj *cjson.JSON, key string, defval string) (value string) {
5559
return GetString(item)
5660
}
5761

62+
func GetItemValue(item *cjson.JSON) (any, bool) {
63+
if item.IsArray() != 0 {
64+
return item, false
65+
} else if item.IsBool() != 0 {
66+
if item.IsTrue() != 0 {
67+
return true, true
68+
}
69+
return false, true
70+
} else if item.IsNumber() != 0 {
71+
return float64(item.GetNumberValue()), true
72+
} else if item.IsObject() != 0 {
73+
return item, false
74+
} else if item.IsNull() != 0 {
75+
return nil, false
76+
} else if item.IsInvalid() != 0 {
77+
return nil, false
78+
} else if item.IsRaw() != 0 {
79+
return item, false
80+
} else if item.IsString() != 0 {
81+
return GetString(item), true
82+
}
83+
return nil, false
84+
}
85+
86+
func GetMapItem[ValueType any](obj *cjson.JSON, mapItemKey string) map[string]ValueType {
87+
cStrOfMapItemKey := c.AllocaCStr(mapItemKey)
88+
mapObj := obj.GetObjectItem(cStrOfMapItemKey)
89+
if mapObj == nil {
90+
return nil
91+
}
92+
m := make(map[string]ValueType)
93+
for child := mapObj.Child; child != nil; child = child.Next {
94+
key := c.GoString(child.String)
95+
value, ok := GetItemValue(child)
96+
if ok {
97+
tValue, ok := value.(ValueType)
98+
if ok {
99+
m[key] = tValue
100+
}
101+
}
102+
}
103+
return m
104+
}
105+
58106
func GetStringArrayItem(obj *cjson.JSON, key string) (value []string) {
59107
item := obj.GetObjectItemCaseSensitive(c.AllocaCStr(key))
60108
if item == nil {

_xtool/llcppsymg/llcppsymg.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func main() {
5858
} else {
5959
fmt.Println("Config From File", ags.CfgFile)
6060
}
61-
fmt.Printf("%s\n", conf.String())
61+
fmt.Printf("%s\n", conf.Config.String())
6262
}
6363

6464
if err != nil {

_xtool/llcppsymg/parse/parse.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,11 @@ type Config struct {
2828

2929
func NewConfig(files, prefixes, cflags []string,
3030
isCpp bool,
31-
symMap []string) *Config {
32-
m := make(map[string]string)
33-
for _, keyVal := range symMap {
34-
strs := strings.Split(keyVal, ":")
35-
if len(strs) == 2 {
36-
key := strings.TrimSpace(strs[0])
37-
val := strings.TrimSpace(strs[1])
38-
m[key] = val
39-
}
31+
symMap map[string]string) *Config {
32+
if symMap == nil {
33+
symMap = make(map[string]string)
4034
}
41-
return &Config{Files: files, Prefixes: prefixes, Cflags: cflags, IsCpp: isCpp, SymMap: m}
35+
return &Config{Files: files, Prefixes: prefixes, Cflags: cflags, IsCpp: isCpp, SymMap: symMap}
4236
}
4337

4438
func (p *Config) String() string {

cmd/llcppcfg/llcppgcfg/cfg_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ func TestNewLLCppConfig(t *testing.T) {
500500
Cplusplus: false,
501501
Impl: []llcppg.ImplFiles{*llcppg.NewImplFiles()},
502502
KeepUnderScore: false,
503-
SymMap: []string{},
503+
SymMap: map[string]string{},
504504
TypeMap: map[string]string{},
505505
},
506506
},

cmd/llcppcfg/llcppgcfg/cfg_test_data/bdw-gc/llcppg.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,6 @@
3737
}
3838
],
3939
"mix": false,
40-
"symMap": [],
40+
"symMap": {},
4141
"typeMap": {}
4242
}

cmd/llcppcfg/llcppgcfg/cfg_test_data/cjson/llcppg.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@
2020
}
2121
],
2222
"mix": false,
23-
"symMap": [],
23+
"symMap": {},
2424
"typeMap": {}
2525
}

cmd/llcppcfg/llcppgcfg/cfg_test_data/libffi/llcppg.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@
1919
}
2020
],
2121
"mix": false,
22-
"symMap": [],
22+
"symMap": {},
2323
"typeMap": {}
2424
}

llcppg/llcppg.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@ type Config struct {
3838
KeepUnderScore bool `json:"keepUnderScore"`
3939
Impl []ImplFiles `json:"impl"`
4040
Mix bool `json:"mix"`
41-
SymMap []string `json:"symMap"`
41+
SymMap map[string]string `json:"symMap"`
4242
TypeMap map[string]string `json:"typeMap"`
4343
}
4444

4545
func NewDefaultConfig() *Config {
46-
cfg := &Config{SymMap: make([]string, 0), TypeMap: make(map[string]string), Impl: []ImplFiles{*NewImplFiles()}}
46+
cfg := &Config{SymMap: make(map[string]string), TypeMap: make(map[string]string), Impl: []ImplFiles{*NewImplFiles()}}
4747
return cfg
4848
}
4949

0 commit comments

Comments
 (0)