No announcement yet.

Emitting nested arrays

  • Filter
  • Time
  • Show
Clear All
new posts

    Emitting nested arrays

    I'm trying to wrap summernote, which expects a nested array of strings to initialize its toolbar, like so:
      toolbar: [
        // [groupName,[list of button]]
        ['style', ['bold', 'italic', 'underline', 'clear']],
        ['font', ['strikethrough', 'superscript', 'subscript']],
        ['fontsize', ['fontsize']],
        ['color', ['color']],
        ['para', ['ul', 'ol', 'paragraph']],
        ['height', ['height']]  
    ] });

    The problem is: I haven't found a clean way to create such a nested array in C#. I tried:
    • Creating an [External] class with two properties of type: string and string[]. But property names are always emitted, so I can't create a pure JS string array. Instead, the result is something like [ { Name: "Group", Buttons: [ "bold", "italic" ]} ]
    • Using Tuple<string, string[]>. This results in "item1" and "item2" properties being emitted
    Any ideas? Thanks, Marco

    Hi Marco,

    The output you posted could be achieved using Bridge.Union<string, string[]>[] type (see Union type declarations here). Probably a cleaner way would be to use Bridge.Sequence<string, string[]> originating from TypeScript Tuple type (Sequence type declarations and description).

    This is how the original sample would look like with usage of Bridge.Sequence (a type alias was used to make the sample even cleaner):

    using Bridge;
    using ButtonsGroup = Bridge.Sequence<string, string[]>;
    public class Program
        public static void Main()
            var config = new Config
                toolbar = new ButtonsGroup[] 
                    new ButtonsGroup("style", new[] {"bold", "italic"}),
                    new ButtonsGroup("font", new[] {"superscript", "subscript"})
    class Config
        public ButtonsGroup[] toolbar;
    Hope that helps.


      Hi Andrey, awesome help as always, thanks! When taking your approach a bit further, I noticed something in Bridge which could be considered a bug. Check out my sample, I just added an additional enum type:

      As you will notice, this sample leads to an exception because of the missing enum type. In my wrapper I'm trying to avoid an additional bridge JS file, which is why I marked the enum as [External]. This should be valid since the enum is decorated with [Enum(Emit.StringNameLowerCase)]. In other words, bridge only exports static string constants anyway, no need for a special type.

      I suppose you should treat such enums as string / string[] in the transpiled JS and remove the type all together.

      Thanks, Marco


        I believe I have found the solution by adding [Name("System.String")] to the enum:


          Thanks for update, nice idea with the enum!

          We'll discuss whether Bridge should treat external enums with [Enum(Emit.StringName*] emit option as just a string type without requirement to specify [Name] attribute explicitly. For now an error appears because Bridge expects (as usual for external entities) ToolbarButton to be defined in JavaScript, whereas it's not. So applying [Name("System.String")] attribute makes Bridge thinking it deals with just existing regular string type.

          Another way to get that issue round is to apply [Virtual] attribute instead of [External]. It works similarly except the part with type resolution. If a virtual type does not exist in JavaScript, Bridge creates a type descriptor for it dynamically.

          The difference between two approaches: [Name("System.String")] and [Virtual] is that a virtual type is still respected in casting. So the next sample will work for [Name] attribute and fail for [Virtual] attribute.
          object o = new[] { ToolbarButton.Bold, ToolbarButton.Italic };
          string[] strs = (string[]) o;