Skip to content

Commit

Permalink
descend as much as necessary to find structs
Browse files Browse the repository at this point in the history
caught these examples in http.Request.TLS:

PeerCertificates []*x509.Certificate
VerifiedChains [][]*x509.Certificate
  • Loading branch information
pbnjay committed Jan 2, 2025
1 parent 9e26774 commit 75fc0f5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 22 deletions.
39 changes: 19 additions & 20 deletions v2/internal/binding/binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,20 +262,17 @@ func (b *Bindings) AddStructToGenerateTS(packageName string, structName string,

// Iterate this struct and add any struct field references
structType := reflect.TypeOf(s)
if hasElements(structType) {
for hasElements(structType) {
structType = structType.Elem()
}

for i := 0; i < structType.NumField(); i++ {
field := structType.Field(i)
if field.Anonymous {
if field.Anonymous || !field.IsExported() {
continue
}
kind := field.Type.Kind()
if kind == reflect.Struct {
if !field.IsExported() {
continue
}
fqname := field.Type.String()
sNameSplit := strings.SplitN(fqname, ".", 2)
if len(sNameSplit) < 2 {
Expand All @@ -288,22 +285,24 @@ func (b *Bindings) AddStructToGenerateTS(packageName string, structName string,
s := reflect.Indirect(a).Interface()
b.AddStructToGenerateTS(pName, sName, s)
}
} else if hasElements(field.Type) && field.Type.Elem().Kind() == reflect.Struct {
if !field.IsExported() {
continue
}
fqname := field.Type.Elem().String()
sNameSplit := strings.SplitN(fqname, ".", 2)
if len(sNameSplit) < 2 {
continue
} else {
fType := field.Type
for hasElements(fType) {
fType = fType.Elem()
}
sName := sNameSplit[1]
pName := getPackageName(fqname)
typ := field.Type.Elem()
a := reflect.New(typ)
if b.hasExportedJSONFields(typ) {
s := reflect.Indirect(a).Interface()
b.AddStructToGenerateTS(pName, sName, s)
if fType.Kind() == reflect.Struct {
fqname := fType.String()
sNameSplit := strings.SplitN(fqname, ".", 2)
if len(sNameSplit) < 2 {
continue
}
sName := sNameSplit[1]
pName := getPackageName(fqname)
a := reflect.New(fType)
if b.hasExportedJSONFields(fType) {
s := reflect.Indirect(a).Interface()
b.AddStructToGenerateTS(pName, sName, s)
}
}
}
}
Expand Down
11 changes: 9 additions & 2 deletions v2/internal/typescriptify/typescriptify.go
Original file line number Diff line number Diff line change
Expand Up @@ -835,7 +835,10 @@ type typeScriptClassBuilder struct {
func (t *typeScriptClassBuilder) AddSimpleArrayField(fieldName string, field reflect.StructField, arrayDepth int, opts TypeOptions) error {
fieldType := nameTypeOf(field.Type.Elem())
kind := field.Type.Elem().Kind()
typeScriptType := t.types[kind]
typeScriptType, ok := t.types[kind]
if !ok {
typeScriptType = "any"
}

if len(fieldName) > 0 {
strippedFieldName := strings.ReplaceAll(fieldName, "?", "")
Expand All @@ -857,7 +860,11 @@ func (t *typeScriptClassBuilder) AddSimpleField(fieldName string, field reflect.
fieldType := nameTypeOf(field.Type)
kind := field.Type.Kind()

typeScriptType := t.types[kind]
typeScriptType, ok := t.types[kind]
if !ok {
typeScriptType = "any"
}

if len(opts.TSType) > 0 {
typeScriptType = opts.TSType
}
Expand Down

0 comments on commit 75fc0f5

Please sign in to comment.