Skip to content

brahms116/between

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

43 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Between

A small language to describe DTOs and sync them between languages.

Warning

Very early prototype, many and all things subject to change as well as many feature gaps

Example

Given the following file demo.bt

prod User {
  age Int,
  name "$name" Str,
  email Str?,
  hobbies []?Str,
  dateOfBirth Date,
  Status,
  UserData,
}

sumstr Status {
  Active,
  Disabled,
  Pending "pending activation",
}

sum UserData {
  AdminData,
  CustomerData,
}

prod AdminData {
  accessLevel Int,
}

prod CustomerData {
  attributes Object,
}

Generates the following TypeScript code

export interface User {
  age: number;
  $name: string;
  email?: string;
  hobbies?: string[];
  dateOfBirth: string;
  status: Status;
  userData: UserData;
}
export type Status = "Active" | "Disabled" | "pending activation";
export type UserData =
  | { adminData: AdminData }
  | { customerData: CustomerData };
export interface AdminData {
  accessLevel: number;
}
export interface CustomerData {
  attributes: Record<string, unknown>;
}

and the following Go code

package demo

import (
	"time"
)

type User struct {
	Age         int       `json:"age"`
	Name        string    `json:"$name"`
	Email       *string   `json:"email,omitEmpty"`
	Hobbies     *[]string `json:"hobbies,omitEmpty"`
	DateOfBirth time.Time `json:"dateOfBirth"`
	Status      Status    `json:"status"`
	UserData    UserData  `json:"userData"`
}
type Status string

const Status_Active Status = "Active"
const Status_Disabled Status = "Disabled"
const Status_Pending Status = "pending activation"

type UserData struct {
	AdminData    *AdminData    `json:"adminData,omitEmpty"`
	CustomerData *CustomerData `json:"customerData,omitEmpty"`
}
type AdminData struct {
	AccessLevel int `json:"accessLevel"`
}
type CustomerData struct {
	Attributes map[string]any `json:"attributes"`
}

TODOs

  • Use runes instead of bytes
  • Split ast and codegen input. Tokens should be part of the AST so that it can contain sugar and locations so it can be used for formatting.
  • Add support for doc comments
  • Add type checking
  • Add LSP support
  • Add support for more languages

Installation

go install github.com/brahms116/between/cmd/bt@latest

Usage

bt --input ./demo.bt --output ./result.go && gofmt -w ./result.go

or

bt --input ./demo.bt --output ./result.ts && prettier --write ./result.ts

About

A small language to describe DTOs and sync them between languages.

Resources

Stars

Watchers

Forks

Packages

No packages published