Skip to content
Open
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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ node_modules

cmd/protoc-gen-cpp-tableau-loader/protoc-gen-cpp-tableau-loader
cmd/protoc-gen-go-tableau-loader/protoc-gen-go-tableau-loader
cmd/protoc-gen-csharp-tableau-loader/protoc-gen-csharp-tableau-loader

test/go-tableau-loader/go-tableau-loader
_lab/ts/src/protoconf
!test/testdata/bin/
test/csharp-tableau-loader/protoconf

13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,19 @@ The official config loader for [Tableau](https://github.com/tableauio/tableau).

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

## C#

### Requirements

- dotnet-sdk-8.0
Copy link
Member

@wenchy wenchy Aug 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


### Test

- Install: **dotnet-sdk-8.0**
- Change dir: `cd test/csharp-tableau-loader`
- Generate protoconf: `sh gen.sh`
- Test: `dotnet run`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


## TypeScript

### Requirements
Expand Down
34 changes: 34 additions & 0 deletions cmd/protoc-gen-csharp-tableau-loader/embed.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package main

import (
"embed"
"path"

"github.com/tableauio/loader/cmd/protoc-gen-csharp-tableau-loader/helper"
"google.golang.org/protobuf/compiler/protogen"
)

//go:embed embed/*
var efs embed.FS

// generateEmbed generates related registry files.
func generateEmbed(gen *protogen.Plugin) {
entries, err := efs.ReadDir("embed")
if err != nil {
panic(err)
}
for _, entry := range entries {
if entry.IsDir() {
continue
}

g := gen.NewGeneratedFile(entry.Name(), "")
helper.GenerateFileHeader(gen, nil, g, version)
// refer: [embed: embed path on different OS cannot open file](https://github.com/golang/go/issues/45230)
content, err := efs.ReadFile(path.Join("embed", entry.Name()))
if err != nil {
panic(err)
}
g.P(string(content))
}
}
112 changes: 112 additions & 0 deletions cmd/protoc-gen-csharp-tableau-loader/embed/Load.pc.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
using pb = global::Google.Protobuf;
using pbr = global::Google.Protobuf.Reflection;
namespace Tableau
{
public static class Load
{
public delegate byte[] ReadFunc(string path);
public delegate pb::IMessage? LoadFunc(pbr::MessageDescriptor desc, string dir, Format fmt, in MessagerOptions? options);

public class BaseOptions
{
public bool? IgnoreUnknownFields { get; set; }
public ReadFunc? ReadFunc { get; set; }
public LoadFunc? LoadFunc { get; set; }
}

public class Options : BaseOptions
{
public Dictionary<string, MessagerOptions>? MessagerOptions { get; set; }
public MessagerOptions ParseMessagerOptionsByName(string name)
{
var mopts = MessagerOptions?.TryGetValue(name, out var val) == true ? (MessagerOptions)val.Clone() : new MessagerOptions();
mopts.IgnoreUnknownFields ??= IgnoreUnknownFields;
mopts.ReadFunc ??= ReadFunc;
mopts.LoadFunc ??= LoadFunc;
return mopts;
}
}

public class MessagerOptions : BaseOptions, ICloneable
{
public string? Path { get; set; }

public object Clone()
{
return new MessagerOptions
{
IgnoreUnknownFields = IgnoreUnknownFields,
ReadFunc = ReadFunc,
LoadFunc = LoadFunc,
Path = Path
};
}
}

public static pb::IMessage? LoadMessager(pbr::MessageDescriptor desc, string path, Format fmt, in MessagerOptions? options = null)
{
var readFunc = options?.ReadFunc ?? File.ReadAllBytes;
byte[] content = readFunc(path);
return Unmarshal(content, desc, fmt, options);
}

public static pb::IMessage? LoadMessagerInDir(pbr::MessageDescriptor desc, string dir, Format fmt, in MessagerOptions? options = null)
{
string name = desc.Name;
string path = "";
if (options?.Path != null)
{
path = options.Path;
fmt = Util.GetFormat(path);
}
if (path == "")
{
string filename = name + Util.Format2Ext(fmt);
path = Path.Combine(dir, filename);
}
var loadFunc = options?.LoadFunc ?? LoadMessager;
return loadFunc(desc, path, fmt, options);
}

public static pb::IMessage? Unmarshal(byte[] content, pbr::MessageDescriptor desc, Format fmt, in MessagerOptions? options = null)
{
switch (fmt)
{
case Format.JSON:
var parser = new pb::JsonParser(
pb::JsonParser.Settings.Default.WithIgnoreUnknownFields(options?.IgnoreUnknownFields ?? false)
);
return parser.Parse(new StreamReader(new MemoryStream(content)), desc);
case Format.Bin:
return desc.Parser.ParseFrom(content);
default:
return null;
}
}
}

public interface IMessagerName
{
static abstract string Name();
}

public abstract class Messager
{
public class Stats
{
public TimeSpan Duration;
}

protected Stats LoadStats = new();

public ref readonly Stats GetStats() => ref LoadStats;

public abstract bool Load(string dir, Format fmt, in Load.MessagerOptions? options = null);

public virtual pb::IMessage? Message() => null;

protected virtual bool ProcessAfterLoad() => true;

public virtual bool ProcessAfterLoadAll(in Hub hub) => true;
}
}
38 changes: 38 additions & 0 deletions cmd/protoc-gen-csharp-tableau-loader/embed/Util.pc.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
namespace Tableau
{
public enum Format
{
Unknown,
JSON,
Bin
}

public static class Util
{

private const string _unknownExt = ".unknown";
private const string _jsonExt = ".json";
private const string _binExt = ".binpb";

public static Format GetFormat(string path)
{
string ext = Path.GetExtension(path);
return ext switch
{
_jsonExt => Format.JSON,
_binExt => Format.Bin,
_ => Format.Unknown,
};
}

public static string Format2Ext(Format fmt)
{
return fmt switch
{
Format.JSON => _jsonExt,
Format.Bin => _binExt,
_ => _unknownExt,
};
}
}
}
97 changes: 97 additions & 0 deletions cmd/protoc-gen-csharp-tableau-loader/embed/templates/Hub.pc.cs.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
using pb = global::Google.Protobuf;
namespace Tableau
{
internal class MessagerContainer(in Dictionary<string, Messager>? messagerMap = null)
{
public Dictionary<string, Messager> MessagerMap = messagerMap ?? [];
public DateTime LastLoadedTime = DateTime.Now;
{{ range . }} public {{ . }}? {{ . }} = InternalGet<{{ . }}>(messagerMap);
{{ end }}
public T? Get<T>() where T : Messager, IMessagerName => InternalGet<T>(MessagerMap);

private static T? InternalGet<T>(in Dictionary<string, Messager>? messagerMap) where T : Messager, IMessagerName =>
messagerMap?.TryGetValue(T.Name(), out var messager) == true ? (T)messager : null;
}

internal class Atomic<T> where T : class
{
private T? _value;

public T? Value
{
get => Interlocked.CompareExchange(ref _value, null, null);
set => Interlocked.Exchange(ref _value, value);
}
}

public class HubOptions
{
public Func<string, bool>? Filter { get; set; }
}

public class Hub(HubOptions? options = null)
{
private readonly Atomic<MessagerContainer> _messagerContainer = new();
private readonly HubOptions? _options = options;

public bool Load(string dir, Format fmt, in Load.Options? options = null)
{
var messagerMap = NewMessagerMap();
var opts = options ?? new Load.Options();
foreach (var kvs in messagerMap)
{
string name = kvs.Key;
if (!kvs.Value.Load(dir, fmt, opts.ParseMessagerOptionsByName(name)))
{
return false;
}
}
var tmpHub = new Hub();
tmpHub.SetMessagerMap(messagerMap);
foreach (var messager in messagerMap.Values)
{
if (!messager.ProcessAfterLoadAll(tmpHub))
{
return false;
}
}
SetMessagerMap(messagerMap);
return true;
}

public IReadOnlyDictionary<string, Messager>? GetMessagerMap() => _messagerContainer.Value?.MessagerMap;

public void SetMessagerMap(in Dictionary<string, Messager> map) => _messagerContainer.Value = new MessagerContainer(map);

public T? Get<T>() where T : Messager, IMessagerName => _messagerContainer.Value?.Get<T>();
{{ range . }}
public {{ . }}? Get{{ . }}() => _messagerContainer.Value?.{{ . }};
{{ end }}
public DateTime? GetLastLoadedTime() => _messagerContainer.Value?.LastLoadedTime;

private Dictionary<string, Messager> NewMessagerMap()
{
var messagerMap = new Dictionary<string, Messager>();
foreach (var kv in Registry.Registrar)
{
if (_options?.Filter?.Invoke(kv.Key) ?? true)
{
messagerMap[kv.Key] = kv.Value();
}
}
return messagerMap;
}
}

public class Registry
{
internal static readonly Dictionary<string, Func<Messager>> Registrar = [];

public static void Register<T>() where T : Messager, IMessagerName, new() => Registrar[T.Name()] = () => new T();

public static void Init()
{
{{ range . }} Register<{{ . }}>();
{{ end }} }
}
}
Loading