Skip to content
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

v3 parser: initial work on model generation w/ templates #2428

Merged
merged 2 commits into from
Feb 28, 2023
Merged
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
40 changes: 40 additions & 0 deletions v3/internal/parser/models.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,45 @@
package parser

import (
"io"
"text/template"
)

type ModelDefinitions struct {
Package string
Models map[string]*StructDef
}

func GenerateModel(wr io.Writer, def *ModelDefinitions) error {
tmpl, err := template.New("model.ts.tmpl").ParseFiles("templates/model.ts.tmpl")
if err != nil {
println("Unable to create class template: " + err.Error())
return err
}

err = tmpl.ExecuteTemplate(wr, "model.ts.tmpl", def)
if err != nil {
println("Problem executing template: " + err.Error())
return err
}
return nil
}

//func GenerateClass(wr io.Writer, def *StructDef) error {
// tmpl, err := template.New("class.ts.tmpl").ParseFiles("templates/class.ts.tmpl")
// if err != nil {
// println("Unable to create class template: " + err.Error())
// return err
// }
//
// err = tmpl.ExecuteTemplate(wr, "class.ts.tmpl", def)
// if err != nil {
// println("Problem executing template: " + err.Error())
// return err
// }
// return nil
//}

//
//import (
// "bytes"
Expand Down
133 changes: 133 additions & 0 deletions v3/internal/parser/models_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package parser

import (
"github.com/google/go-cmp/cmp"
"strings"
"testing"
)

const expected = `
export namespace main {

export class Person {
name: string;
parent: Person;
details: anon1;
address: package.Address;

static createFrom(source: any = {}) {
return new Person(source);
}

constructor(source: any = {}) {
if ('string' === typeof source) {
source = JSON.parse(source);
}

this.name = source["name"]
this.parent = source["parent"]
this.details = source["details"]
this.address = source["address"]

}
}

export class anon1 {
age: int;
address: string;

static createFrom(source: any = {}) {
return new anon1(source);
}

constructor(source: any = {}) {
if ('string' === typeof source) {
source = JSON.parse(source);
}

this.age = source["age"]
this.address = source["address"]

}
}

}
`

func TestGenerateClass(t *testing.T) {
person := StructDef{
Name: "Person",
Fields: []*Field{
{
Name: "Name",
Type: &ParameterType{
Name: "string",
},
},
{
Name: "Parent",
Type: &ParameterType{
Name: "Person",
IsStruct: true,
IsPointer: true,
Package: "main",
},
},
{
Name: "Details",
Type: &ParameterType{
Name: "anon1",
IsStruct: true,
Package: "main",
},
},
{
Name: "Address",
Type: &ParameterType{
Name: "Address",
IsStruct: true,
IsPointer: true,
Package: "github.com/some/other/package",
},
},
},
}
anon1 := StructDef{
Name: "anon1",
Fields: []*Field{
{
Name: "Age",
Type: &ParameterType{
Name: "int",
},
},
{
Name: "Address",
Type: &ParameterType{
Name: "string",
},
},
},
}

var builder strings.Builder
models := make(map[string]*StructDef)
models["Person"] = &person
models["anon1"] = &anon1
def := ModelDefinitions{
Package: "main",
Models: models,
}

err := GenerateModel(&builder, &def)
if err != nil {
t.Fatal(err)
}

text := builder.String()
println("Built string")
println(text)
if diff := cmp.Diff(expected, text); diff != "" {
t.Errorf("GenerateClass() failed:\n" + diff)
}
}
16 changes: 16 additions & 0 deletions v3/internal/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"path/filepath"
"reflect"
"strconv"
"strings"
)

type packagePath = string
Expand Down Expand Up @@ -49,6 +50,21 @@ type Field struct {
Type *ParameterType
}

func (f *Field) JSName() string {
return strings.ToLower(f.Name[0:1]) + f.Name[1:]
}

func (f *Field) JSDef(pkg string) string {
name := f.JSName()

if f.Type.Package == "" || f.Type.Package == pkg {
return fmt.Sprintf("%s: %s;", name, f.Type.Name)
}

parts := strings.Split(f.Type.Package, "/")
return fmt.Sprintf("%s: %s.%s;", name, parts[len(parts)-1], f.Type.Name)
}

type ParsedPackage struct {
Pkg *ast.Package
Name string
Expand Down
16 changes: 16 additions & 0 deletions v3/internal/parser/templates/class.ts.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export class {{.Name}} {
{{range .Fields}}{{.}}
{{end}}
static createFrom(source: any = {}) {
return new {{.Name}}(source);
}

constructor(source: any = {}) {
if ('string' === typeof source) {
source = JSON.parse(source);
}

{{range .Fields}}this.{{jsName .}} = source["{{jsName .}}"]
{{end}}
}
}
21 changes: 21 additions & 0 deletions v3/internal/parser/templates/model.ts.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{{$pkg := .Package}}
export namespace {{.Package}} {
{{range $name, $def := .Models}}
export class {{$def.Name}} {
{{range $def.Fields}}{{.JSDef $pkg}}
{{end}}
static createFrom(source: any = {}) {
return new {{$def.Name}}(source);
}

constructor(source: any = {}) {
if ('string' === typeof source) {
source = JSON.parse(source);
}

{{range $def.Fields}}this.{{.JSName}} = source["{{.JSName}}"]
{{end}}
}
}
{{end}}
}