Skip to content

Commit 6ede41d

Browse files
committed
feat(cl/nodeconv):with condition go file
1 parent a1730b0 commit 6ede41d

3 files changed

Lines changed: 34 additions & 21 deletions

File tree

cl/nc/ncimpl/ncimpl.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ type Converter struct {
4444
KeepUnderScore bool
4545
}
4646

47-
func (p *Converter) convFile(file string, obj *ast.Object) (goFile string, ok bool) {
47+
func (p *Converter) convFile(file string, obj *ast.Object) (goFile *nc.GoFile, ok bool) {
4848
info, exist := p.FileMap[file]
4949
if !exist {
5050
var availableFiles []string
@@ -58,10 +58,13 @@ func (p *Converter) convFile(file string, obj *ast.Object) (goFile string, ok bo
5858
if obj != nil && obj.Name != nil && hf.FileType == llconfig.Third {
5959
p.locMap.Add(obj.Name, obj.Loc)
6060
}
61-
return hf.ToGoFileName(p.PkgName), hf.InCurPkg()
61+
return &nc.GoFile{
62+
FileName: hf.ToGoFileName(p.PkgName),
63+
// todo(zzy):support condition
64+
}, hf.InCurPkg()
6265
}
6366

64-
func (p *Converter) ConvDecl(file string, decl ast.Decl) (goName, goFile string, err error) {
67+
func (p *Converter) ConvDecl(file string, decl ast.Decl) (goName string, goFile *nc.GoFile, err error) {
6568
obj := ast.ObjectOf(decl)
6669
goFile, ok := p.convFile(file, obj)
6770
if !ok {
@@ -88,7 +91,7 @@ func (p *Converter) ConvDecl(file string, decl ast.Decl) (goName, goFile string,
8891
return
8992
}
9093

91-
func (p *Converter) ConvMacro(file string, macro *ast.Macro) (goName, goFile string, err error) {
94+
func (p *Converter) ConvMacro(file string, macro *ast.Macro) (goName string, goFile *nc.GoFile, err error) {
9295
goFile, ok := p.convFile(file, nil)
9396
if !ok {
9497
err = nc.ErrSkip

cl/nc/ncimpl/ncimpl_test.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,8 @@ func TestConverterConvFile(t *testing.T) {
169169
t.Run(tc.name, func(t *testing.T) {
170170
goFile, ok := converter.convFile(tc.file, nil)
171171

172-
if goFile != tc.expected {
173-
t.Errorf("Expected file %s, got %s", tc.expected, goFile)
172+
if goFile.FileName != tc.expected {
173+
t.Errorf("Expected file %s, got %s", tc.expected, goFile.FileName)
174174
}
175175

176176
if ok != tc.ok {
@@ -226,7 +226,7 @@ func TestConv(t *testing.T) {
226226
trimPrefixes []string
227227
expectFile string
228228
expectName string
229-
exec func(conv *Converter, convNode ast.Node) (string, string, error)
229+
exec func(conv *Converter, convNode ast.Node) (string, *nc.GoFile, error)
230230
expectErr error
231231
}{
232232
{
@@ -235,7 +235,7 @@ func TestConv(t *testing.T) {
235235
Name: "MACRO_NAME",
236236
Loc: &ast.Location{File: interFile},
237237
},
238-
exec: func(conv *Converter, convNode ast.Node) (string, string, error) {
238+
exec: func(conv *Converter, convNode ast.Node) (string, *nc.GoFile, error) {
239239
return conv.ConvMacro(interFile, convNode.(*ast.Macro))
240240
},
241241
expectFile: "inter.go",
@@ -248,7 +248,7 @@ func TestConv(t *testing.T) {
248248
Name: "MACRO_NAME",
249249
Loc: &ast.Location{File: interFile},
250250
},
251-
exec: func(conv *Converter, convNode ast.Node) (string, string, error) {
251+
exec: func(conv *Converter, convNode ast.Node) (string, *nc.GoFile, error) {
252252
return conv.ConvMacro(interFile, convNode.(*ast.Macro))
253253
},
254254
trimPrefixes: []string{"MACRO_"},
@@ -263,7 +263,7 @@ func TestConv(t *testing.T) {
263263
Loc: &ast.Location{File: interFile},
264264
},
265265
pubs: map[string]string{"MACRO_CONST": "CustomMacro"},
266-
exec: func(conv *Converter, convNode ast.Node) (string, string, error) {
266+
exec: func(conv *Converter, convNode ast.Node) (string, *nc.GoFile, error) {
267267
return conv.ConvMacro(interFile, convNode.(*ast.Macro))
268268
},
269269
expectFile: "inter.go",
@@ -276,7 +276,7 @@ func TestConv(t *testing.T) {
276276
Name: "MACRO_NAME",
277277
Loc: &ast.Location{File: thirdFile},
278278
},
279-
exec: func(conv *Converter, convNode ast.Node) (string, string, error) {
279+
exec: func(conv *Converter, convNode ast.Node) (string, *nc.GoFile, error) {
280280
return conv.ConvMacro(thirdFile, convNode.(*ast.Macro))
281281
},
282282
expectFile: "testpkg_autogen.go",
@@ -292,7 +292,7 @@ func TestConv(t *testing.T) {
292292
MangledName: "validFunc",
293293
},
294294
convSym: mockSymConv,
295-
exec: func(conv *Converter, convNode ast.Node) (string, string, error) {
295+
exec: func(conv *Converter, convNode ast.Node) (string, *nc.GoFile, error) {
296296
return conv.ConvDecl(interFile, convNode.(*ast.FuncDecl))
297297
},
298298
expectFile: "inter.go",
@@ -307,7 +307,7 @@ func TestConv(t *testing.T) {
307307
Loc: &ast.Location{File: thirdFile},
308308
},
309309
},
310-
exec: func(conv *Converter, convNode ast.Node) (string, string, error) {
310+
exec: func(conv *Converter, convNode ast.Node) (string, *nc.GoFile, error) {
311311
return conv.ConvDecl(thirdFile, convNode.(*ast.FuncDecl))
312312
},
313313
expectFile: "testpkg_autogen.go",
@@ -322,7 +322,7 @@ func TestConv(t *testing.T) {
322322
},
323323
MangledName: "noSymbolFunc",
324324
},
325-
exec: func(conv *Converter, convNode ast.Node) (string, string, error) {
325+
exec: func(conv *Converter, convNode ast.Node) (string, *nc.GoFile, error) {
326326
return conv.ConvDecl(interFile, convNode.(*ast.FuncDecl))
327327
},
328328
convSym: mockSymConv,
@@ -337,7 +337,7 @@ func TestConv(t *testing.T) {
337337
Loc: &ast.Location{File: interFile},
338338
},
339339
},
340-
exec: func(conv *Converter, convNode ast.Node) (string, string, error) {
340+
exec: func(conv *Converter, convNode ast.Node) (string, *nc.GoFile, error) {
341341
return conv.ConvDecl(interFile, convNode.(*ast.EnumTypeDecl))
342342
},
343343
expectFile: "inter.go",
@@ -352,7 +352,7 @@ func TestConv(t *testing.T) {
352352
Loc: &ast.Location{File: interFile},
353353
},
354354
},
355-
exec: func(conv *Converter, convNode ast.Node) (string, string, error) {
355+
exec: func(conv *Converter, convNode ast.Node) (string, *nc.GoFile, error) {
356356
return conv.ConvDecl(interFile, convNode.(*ast.EnumTypeDecl))
357357
},
358358
expectFile: "inter.go",
@@ -366,7 +366,7 @@ func TestConv(t *testing.T) {
366366
Loc: &ast.Location{File: interFile},
367367
},
368368
},
369-
exec: func(conv *Converter, convNode ast.Node) (string, string, error) {
369+
exec: func(conv *Converter, convNode ast.Node) (string, *nc.GoFile, error) {
370370
return conv.ConvDecl(interFile, convNode.(*ast.TypeDecl))
371371
},
372372
expectFile: "inter.go",
@@ -388,8 +388,8 @@ func TestConv(t *testing.T) {
388388
if goName != tc.expectName {
389389
t.Errorf("Expected %s, got %s", tc.expectName, goName)
390390
}
391-
if goFile != tc.expectFile {
392-
t.Errorf("Expected %s, got %s", tc.expectFile, goFile)
391+
if goFile.FileName != tc.expectFile {
392+
t.Errorf("Expected %s, got %s", tc.expectFile, goFile.FileName)
393393
}
394394
if err != tc.expectErr {
395395
t.Errorf("Expected %v, got %v", tc.expectErr, err)

cl/nc/nodeconv.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,19 @@ var (
1111
ErrSkip = errors.New("skip this node")
1212
)
1313

14+
type Condition struct {
15+
OS string // OS,like darwin,linux,windows
16+
Arch string // Architecture,like amd64,arm64
17+
}
18+
19+
type GoFile struct {
20+
FileName string // Go file name,like cJSON.go ini_darwin_amd64.go
21+
Condition *Condition // Condition for the given file,if no condition,it is nil
22+
}
23+
1424
type NodeConverter interface {
15-
ConvDecl(file string, decl ast.Decl) (goName, goFile string, err error)
16-
ConvMacro(file string, macro *ast.Macro) (goName, goFile string, err error)
25+
ConvDecl(file string, decl ast.Decl) (goName string, goFile *GoFile, err error)
26+
ConvMacro(file string, macro *ast.Macro) (goName string, goFile *GoFile, err error)
1727
ConvEnumItem(decl *ast.EnumTypeDecl, item *ast.EnumItem) (goName string, err error)
1828
ConvTagExpr(cname string) string
1929
Lookup(name string) (locFile string, ok bool)

0 commit comments

Comments
 (0)