5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-12 15:09:33 +08:00

v3 parser: misc simplification and cleanup

This commit is contained in:
Adam Tenderholt 2023-03-12 19:34:40 -07:00
parent 90bcefcadb
commit be1624ca9f
2 changed files with 7 additions and 141 deletions

View File

@ -37,11 +37,6 @@ const modelsHeader = `// @ts-check
// This file is automatically generated. DO NOT EDIT
`
type pkgKeys struct {
fullPkg string
alias string
}
func pkgAlias(fullPkg string) string {
pkgParts := strings.Split(fullPkg, "/")
return pkgParts[len(pkgParts)-1]
@ -51,24 +46,21 @@ func GenerateModels(models map[packagePath]map[structName]*StructDef) (string, e
var buffer bytes.Buffer
buffer.WriteString(modelsHeader)
// to sort pkgs by alias, need to collect fullPkg and the import alias
// sort pkgs by alias (e.g. services) instead of full pkg name (e.g. github.com/wailsapp/wails/somedir/services)
// and then sort resulting list by the alias
var keys []pkgKeys
var keys []string
for pkg, _ := range models {
keys = append(keys, pkgKeys{
fullPkg: pkg,
alias: pkgAlias(pkg),
})
keys = append(keys, pkg)
}
sort.Slice(keys, func(i, j int) bool {
return keys[i].alias < keys[j].alias
return pkgAlias(keys[i]) < pkgAlias(keys[j])
})
for _, pkgKey := range keys {
for _, pkg := range keys {
err := GenerateModel(&buffer, &ModelDefinitions{
Package: pkgKey.alias,
Models: models[pkgKey.fullPkg],
Package: pkgAlias(pkg),
Models: models[pkg],
})
if err != nil {
return "", err

View File

@ -7,132 +7,6 @@ import (
"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)
// }
//}
func TestGenerateModels(t *testing.T) {
tests := []struct {