-
Notifications
You must be signed in to change notification settings - Fork 5
C#: implement C# loader plugin #98
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Kybxd
wants to merge
1
commit into
master
Choose a base branch
from
csharp-loader
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
|
||
| ### Test | ||
|
|
||
| - Install: **dotnet-sdk-8.0** | ||
| - Change dir: `cd test/csharp-tableau-loader` | ||
| - Generate protoconf: `sh gen.sh` | ||
| - Test: `dotnet run` | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add the C# code style, which we should obey: |
||
|
|
||
| ## TypeScript | ||
|
|
||
| ### Requirements | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
97
cmd/protoc-gen-csharp-tableau-loader/embed/templates/Hub.pc.cs.tpl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 }} } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
About C# language version, compiler version, and runtime version:
We should constrain them accurately.