cmudig / apigen

API Generator from TypeScript
BSD 3-Clause "New" or "Revised" License
3 stars 0 forks source link

Generated Class Format #23

Open lan-lyu opened 1 year ago

lan-lyu commented 1 year ago

I create 2 versions of generated data class example and its test.

Folder Structure

Each folder has 3 files

  1. __util__.ts: definiton of BaseObejct and methods
  2. data.ts: generated data class from TopLevelUnitSpec type
  3. test.ts: import methods in data.ts and generate JSON

Details

Data class with generic looks like this

class Data extends BaseObject<TopLevelUnitSpec<Field>> {

    constructor(value: TopLevelUnitSpec<Field>) {
        super();
        init(this);
        set(this, "data", value);
    }

When run test.ts

import * as VL from "./data";
const spec = VL.data("example.csv").autosize("fit").background("red").bounds("full");
console.log(VL.toJSON(spec));

The output is

{"data":"example.csv","autosize":"fit","background":"red","bounds":"full"}
domoritz commented 1 year ago

The generic version looks like a good start. Ideally we switch more of the any to be concrete types (for example in the get method). That will make the code safer to use and easier to debug. However, feel free to just add todo comments if you run into troubles and I can help.