mirror of
https://github.com/wailsapp/wails.git
synced 2025-05-02 22:13:36 +08:00
Feature: TS namespaces
This commit is contained in:
parent
a65a40fb4c
commit
625eca27f6
@ -87,6 +87,7 @@ func (b *Bindings) WriteModels(modelsDir string) error {
|
|||||||
thisPackageCode := ""
|
thisPackageCode := ""
|
||||||
for _, structInterface := range structsToGenerate {
|
for _, structInterface := range structsToGenerate {
|
||||||
w := typescriptify.New()
|
w := typescriptify.New()
|
||||||
|
w.Namespace = packageName
|
||||||
w.WithBackupDir("")
|
w.WithBackupDir("")
|
||||||
w.Add(structInterface)
|
w.Add(structInterface)
|
||||||
str, err := w.Convert(nil)
|
str, err := w.Convert(nil)
|
||||||
@ -121,5 +122,40 @@ func (b *Bindings) AddStructToGenerateTS(packageName string, structName string,
|
|||||||
if b.structsToGenerateTS[packageName] == nil {
|
if b.structsToGenerateTS[packageName] == nil {
|
||||||
b.structsToGenerateTS[packageName] = make(map[string]interface{})
|
b.structsToGenerateTS[packageName] = make(map[string]interface{})
|
||||||
}
|
}
|
||||||
|
if b.structsToGenerateTS[packageName][structName] != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
b.structsToGenerateTS[packageName][structName] = s
|
b.structsToGenerateTS[packageName][structName] = s
|
||||||
|
|
||||||
|
// Iterate this struct and add any struct field references
|
||||||
|
structType := reflect.TypeOf(s)
|
||||||
|
if structType.Kind() == reflect.Ptr {
|
||||||
|
structType = structType.Elem()
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < structType.NumField(); i++ {
|
||||||
|
field := structType.Field(i)
|
||||||
|
if field.Anonymous {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
kind := field.Type.Kind()
|
||||||
|
if kind == reflect.Struct {
|
||||||
|
fqname := field.Type.String()
|
||||||
|
sName := strings.Split(fqname, ".")[1]
|
||||||
|
pName := getPackageName(fqname)
|
||||||
|
a := reflect.New(field.Type)
|
||||||
|
s := reflect.Indirect(a).Interface()
|
||||||
|
b.AddStructToGenerateTS(pName, sName, s)
|
||||||
|
} else if kind == reflect.Ptr && field.Type.Elem().Kind() == reflect.Struct {
|
||||||
|
fqname := field.Type.String()
|
||||||
|
sName := strings.Split(fqname, ".")[1]
|
||||||
|
pName := getPackageName(fqname)
|
||||||
|
typ := field.Type.Elem()
|
||||||
|
a := reflect.New(typ)
|
||||||
|
s := reflect.Indirect(a).Interface()
|
||||||
|
b.AddStructToGenerateTS(pName, sName, s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -96,6 +96,8 @@ type TypeScriptify struct {
|
|||||||
|
|
||||||
// throwaway, used when converting
|
// throwaway, used when converting
|
||||||
alreadyConverted map[reflect.Type]bool
|
alreadyConverted map[reflect.Type]bool
|
||||||
|
|
||||||
|
Namespace string
|
||||||
}
|
}
|
||||||
|
|
||||||
func New() *TypeScriptify {
|
func New() *TypeScriptify {
|
||||||
@ -554,6 +556,12 @@ func (t *TypeScriptify) convertType(depth int, typeOf reflect.Type, customCode m
|
|||||||
return "", nil
|
return "", nil
|
||||||
}
|
}
|
||||||
t.logf(depth, "Converting type %s", typeOf.String())
|
t.logf(depth, "Converting type %s", typeOf.String())
|
||||||
|
if strings.ContainsRune(typeOf.String(), '.') {
|
||||||
|
namespace := strings.Split(typeOf.String(), ".")[0]
|
||||||
|
if namespace != t.Namespace {
|
||||||
|
return "", nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
t.alreadyConverted[typeOf] = true
|
t.alreadyConverted[typeOf] = true
|
||||||
|
|
||||||
@ -568,10 +576,11 @@ func (t *TypeScriptify) convertType(depth int, typeOf reflect.Type, customCode m
|
|||||||
result = "export " + result
|
result = "export " + result
|
||||||
}
|
}
|
||||||
builder := typeScriptClassBuilder{
|
builder := typeScriptClassBuilder{
|
||||||
types: t.kinds,
|
types: t.kinds,
|
||||||
indent: t.Indent,
|
indent: t.Indent,
|
||||||
prefix: t.Prefix,
|
prefix: t.Prefix,
|
||||||
suffix: t.Suffix,
|
suffix: t.Suffix,
|
||||||
|
namespace: t.Namespace,
|
||||||
}
|
}
|
||||||
|
|
||||||
fields := deepFields(typeOf)
|
fields := deepFields(typeOf)
|
||||||
@ -731,6 +740,7 @@ type typeScriptClassBuilder struct {
|
|||||||
createFromMethodBody []string
|
createFromMethodBody []string
|
||||||
constructorBody []string
|
constructorBody []string
|
||||||
prefix, suffix string
|
prefix, suffix string
|
||||||
|
namespace string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *typeScriptClassBuilder) AddSimpleArrayField(fieldName string, field reflect.StructField, arrayDepth int, opts TypeOptions) error {
|
func (t *typeScriptClassBuilder) AddSimpleArrayField(fieldName string, field reflect.StructField, arrayDepth int, opts TypeOptions) error {
|
||||||
@ -785,10 +795,14 @@ func (t *typeScriptClassBuilder) AddEnumField(fieldName string, field reflect.St
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t *typeScriptClassBuilder) AddStructField(fieldName string, field reflect.StructField) {
|
func (t *typeScriptClassBuilder) AddStructField(fieldName string, field reflect.StructField) {
|
||||||
fieldType := field.Type.Name()
|
|
||||||
strippedFieldName := strings.ReplaceAll(fieldName, "?", "")
|
strippedFieldName := strings.ReplaceAll(fieldName, "?", "")
|
||||||
t.addField(fieldName, t.prefix+fieldType+t.suffix)
|
namespace := strings.Split(field.Type.String(), ".")[0]
|
||||||
t.addInitializerFieldLine(strippedFieldName, fmt.Sprintf("this.convertValues(source[\"%s\"], %s)", strippedFieldName, t.prefix+fieldType+t.suffix))
|
fqname := field.Type.Name()
|
||||||
|
if namespace != t.namespace {
|
||||||
|
fqname = field.Type.String()
|
||||||
|
}
|
||||||
|
t.addField(fieldName, fqname)
|
||||||
|
t.addInitializerFieldLine(strippedFieldName, fmt.Sprintf("this.convertValues(source[\"%s\"], %s)", strippedFieldName, fqname))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *typeScriptClassBuilder) AddArrayOfStructsField(fieldName string, field reflect.StructField, arrayDepth int) {
|
func (t *typeScriptClassBuilder) AddArrayOfStructsField(fieldName string, field reflect.StructField, arrayDepth int) {
|
||||||
|
Loading…
Reference in New Issue
Block a user