Skip to content

Commit 9a5d600

Browse files
committed
feat: init proj
feat: update go.mod and readme feat: add some template code feat: load from bin feat: only support json and bin feat: hub feat: load methods feat: add filter huboptions feat: a simple example feat: git ignore feat: remove LoadMode
1 parent 3da116e commit 9a5d600

16 files changed

Lines changed: 1234 additions & 2 deletions

File tree

.gitignore

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,22 @@
2727
*.out
2828
*.app
2929

30-
# custom files and directories
30+
# custom files and directories
3131
third_party/_submodules
3232
_out/
3333
build/
3434
bin/
35+
obj/
3536
.idea
3637
node_modules
3738

3839
*.pb.*
3940

4041
cmd/protoc-gen-cpp-tableau-loader/protoc-gen-cpp-tableau-loader
4142
cmd/protoc-gen-go-tableau-loader/protoc-gen-go-tableau-loader
43+
cmd/protoc-gen-csharp-tableau-loader/protoc-gen-csharp-tableau-loader
4244

4345
test/go-tableau-loader/go-tableau-loader
44-
_lab/ts/src/protoconf
46+
_lab/ts/src/protoconf
47+
!test/testdata/bin/
48+
test/csharp-tableau-loader/protoconf

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,19 @@ The official config loader for [Tableau](https://github.com/tableauio/tableau).
3535

3636
- [Protocol Buffers Go Reference](https://protobuf.dev/reference/go/)
3737

38+
## C#
39+
40+
### Requirements
41+
42+
- dotnet-sdk-8.0
43+
44+
### Test
45+
46+
- Change dir: `cd test/csharp-tableau-loader`
47+
- Install depedencies: `sudo yum install dotnet-sdk-8.0 -y`
48+
- Generate protoconf: `sh gen.sh`
49+
- Test: `dotnet run`
50+
3851
## TypeScript
3952

4053
### Requirements
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
"google.golang.org/protobuf/compiler/protogen"
7+
)
8+
9+
func generateFileHeader(gen *protogen.Plugin, file *protogen.File, g *protogen.GeneratedFile) {
10+
generateCommonHeader(gen, g)
11+
if file.Proto.GetOptions().GetDeprecated() {
12+
g.P("// ", file.Desc.Path(), " is a deprecated file.")
13+
} else {
14+
g.P("// source: ", file.Desc.Path())
15+
}
16+
}
17+
18+
func generateCommonHeader(gen *protogen.Plugin, g *protogen.GeneratedFile) {
19+
g.P("// Code generated by protoc-gen-csharp-tableau-loader. DO NOT EDIT.")
20+
g.P("// versions:")
21+
g.P("// - protoc-gen-csharp-tableau-loader v", version)
22+
g.P("// - protoc ", protocVersion(gen))
23+
}
24+
25+
func protocVersion(gen *protogen.Plugin) string {
26+
v := gen.Request.GetCompilerVersion()
27+
if v == nil {
28+
return "(unknown)"
29+
}
30+
var suffix string
31+
if s := v.GetSuffix(); s != "" {
32+
suffix = "-" + s
33+
}
34+
return fmt.Sprintf("v%d.%d.%d%s", v.GetMajor(), v.GetMinor(), v.GetPatch(), suffix)
35+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package helper
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
7+
"github.com/iancoleman/strcase"
8+
"github.com/tableauio/tableau/proto/tableaupb"
9+
"google.golang.org/protobuf/compiler/protogen"
10+
"google.golang.org/protobuf/proto"
11+
"google.golang.org/protobuf/reflect/protoreflect"
12+
"google.golang.org/protobuf/types/descriptorpb"
13+
)
14+
15+
// ParseCsharpType converts a FieldDescriptor to C# type string.
16+
func ParseCsharpType(fd protoreflect.FieldDescriptor) string {
17+
switch fd.Kind() {
18+
case protoreflect.BoolKind:
19+
return "bool"
20+
case protoreflect.EnumKind:
21+
fullname := string(fd.Enum().FullName())
22+
seps := strings.Split(fullname, ".")
23+
seps[0] = strcase.ToCamel(seps[0])
24+
for i := 2; i < len(seps); i++ {
25+
seps[i] = "Types." + seps[i]
26+
}
27+
return strings.Join(seps, ".")
28+
case protoreflect.Int32Kind, protoreflect.Sint32Kind, protoreflect.Sfixed32Kind:
29+
return "int"
30+
case protoreflect.Uint32Kind, protoreflect.Fixed32Kind:
31+
return "uint"
32+
case protoreflect.Int64Kind, protoreflect.Sint64Kind, protoreflect.Sfixed64Kind:
33+
return "long"
34+
case protoreflect.Uint64Kind, protoreflect.Fixed64Kind:
35+
return "ulong"
36+
case protoreflect.FloatKind:
37+
return "float"
38+
case protoreflect.DoubleKind:
39+
return "double"
40+
case protoreflect.StringKind, protoreflect.BytesKind:
41+
return "string"
42+
case protoreflect.MessageKind:
43+
fullname := string(fd.Message().FullName())
44+
seps := strings.Split(fullname, ".")
45+
seps[0] = strcase.ToCamel(seps[0])
46+
for i := 2; i < len(seps); i++ {
47+
seps[i] = "Types." + seps[i]
48+
}
49+
return strings.Join(seps, ".")
50+
// case protoreflect.GroupKind:
51+
// return "group"
52+
default:
53+
return fmt.Sprintf("<unknown:%d>", fd.Kind())
54+
}
55+
}
56+
57+
type MapKey struct {
58+
Type string
59+
Name string
60+
}
61+
62+
func AddMapKey(gen *protogen.Plugin, fd protoreflect.FieldDescriptor, keys []MapKey) []MapKey {
63+
opts := fd.Options().(*descriptorpb.FieldOptions)
64+
fdOpts := proto.GetExtension(opts, tableaupb.E_Field).(*tableaupb.FieldOptions)
65+
name := fdOpts.GetKey()
66+
if fd.MapValue().Kind() == protoreflect.MessageKind {
67+
valueFd := fd.MapValue().Message().Fields().Get(0)
68+
name = string(valueFd.Name())
69+
}
70+
// name = escapeIdentifier(name)
71+
if name == "" {
72+
name = fmt.Sprintf("key%d", len(keys)+1)
73+
} else {
74+
for _, key := range keys {
75+
if key.Name == name {
76+
// rewrite to avoid name confict
77+
name = fmt.Sprintf("%s%d", name, len(keys)+1)
78+
break
79+
}
80+
}
81+
}
82+
keys = append(keys, MapKey{ParseCsharpType(fd.MapKey()), strcase.ToLowerCamel(name)})
83+
return keys
84+
}
85+
86+
// GenGetParams generates function parameters, which are the names listed in the function's definition.
87+
func GenGetParams(keys []MapKey) string {
88+
var params string
89+
for i, key := range keys {
90+
params += key.Type + " " + key.Name
91+
if i != len(keys)-1 {
92+
params += ", "
93+
}
94+
}
95+
return params
96+
}
97+
98+
// GenGetArguments generates function arguments, which are the real values passed to the function.
99+
func GenGetArguments(keys []MapKey) string {
100+
var params string
101+
for i, key := range keys {
102+
params += key.Name
103+
if i != len(keys)-1 {
104+
params += ", "
105+
}
106+
}
107+
return params
108+
}

0 commit comments

Comments
 (0)