Skip to content

Commit 6574a6e

Browse files
authored
Handle extended anonymous structs. (#13)
* Handle extended anonymous structs * deal with linter
1 parent 524443c commit 6574a6e

File tree

7 files changed

+55
-22
lines changed

7 files changed

+55
-22
lines changed

.github/workflows/codetests.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ jobs:
4242
- name: golangci-lint
4343
uses: golangci/golangci-lint-action@v8
4444
with:
45-
version: v2.1
45+
version: v2.4
4646
# Runs golangci-lint on linux against linux and windows.
4747
golangci-linux:
4848
strategy:
@@ -60,4 +60,4 @@ jobs:
6060
- name: golangci-lint
6161
uses: golangci/golangci-lint-action@v8
6262
with:
63-
version: v2.1
63+
version: v2.4

.golangci.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1-
version: "2"
1+
version: '2'
22
linters:
33
default: all
44
disable:
55
- nlreturn
66
- exhaustruct
7+
- wsl
78
settings:
9+
wsl_v5:
10+
allow-first-in-block: true
11+
allow-whole-block: false
12+
branch-max-lines: 2
813
errcheck:
914
disable-default-exclusions: true
1015
exclude-functions:

builder.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ type StructMember struct {
6868
Type string
6969
// Members is a list of members in this member if it's an anonymous struct.
7070
Members []*StructMember
71+
// Extends is a list of struct names that this member extends if it's a struct with anonymous members.
72+
Extends []string
7173
// Member is the struct field that we are building the typescript interface for.
7274
Member reflect.StructField
7375
// Optional is true if the member is optional.
@@ -304,6 +306,8 @@ func (g *Goty) checkStruct(field reflect.Type, member *StructMember) string {
304306
structMember := g.parseStruct(field)
305307
if structMember.Name == "" { // Embedded struct.
306308
member.Members = append(member.Members, structMember.Members...)
309+
member.Extends = append(member.Extends, structMember.Extends...)
310+
307311
return "" // embedded structs don't have names; deal with it.
308312
}
309313

entry.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,14 @@ const (
3333

3434
// Config is the input config for the builder.
3535
type Config struct {
36+
// DocHandler is the handler for go/doc comments. Comments are off by default.
37+
gotyface.Docs `json:"-" toml:"-" xml:"-" yaml:"-"`
38+
3639
// Overrides is a map of go types to their typescript type and name.
3740
// These override the global overrides.
3841
Overrides Overrides `json:"overrides" toml:"overrides" xml:"overrides" yaml:"overrides"`
3942
// GlobalOverrides are applied to all structs unless a type-specific override exists.
4043
GlobalOverrides Override `json:"globalOverrides" toml:"global_overrides" xml:"global-override" yaml:"globalOverrides"`
41-
// DocHandler is the handler for go/doc comments. Comments are off by default.
42-
gotyface.Docs `json:"-" toml:"-" xml:"-" yaml:"-"`
4344
}
4445

4546
// Overrides is a map of go types to their typescript override values.

gotydoc/docs.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ func (d *Docs) AddPkg(src string, pkg string) error {
5353
// AddPkgMust adds a package to the handler's index like AddPkg but panics if there is an error.
5454
// See AddPkg for more details.
5555
func (d *Docs) AddPkgMust(src string, pkg string) *Docs {
56-
if err := d.AddPkg(src, pkg); err != nil {
56+
err := d.AddPkg(src, pkg)
57+
if err != nil {
5758
panic(err)
5859
}
5960

@@ -66,7 +67,8 @@ func (d *Docs) AddPkgMust(src string, pkg string) *Docs {
6667
// Running `go mod vendor` is a good way to create this folder.
6768
func (d *Docs) Add(vendorFolder string, pkg ...string) error {
6869
for _, p := range pkg {
69-
if err := d.AddPkg(filepath.Join(vendorFolder, p), p); err != nil {
70+
err := d.AddPkg(filepath.Join(vendorFolder, p), p)
71+
if err != nil {
7072
return err
7173
}
7274
}

printer.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ func (g *Goty) Write(fileName string, overwrite bool) error {
2727
return ErrNoStructs
2828
}
2929

30-
if _, err := os.Stat(fileName); !os.IsNotExist(err) && !overwrite {
30+
_, err := os.Stat(fileName)
31+
if !os.IsNotExist(err) && !overwrite {
3132
return fmt.Errorf("file exists: %s: %w", fileName, os.ErrExist)
3233
}
3334

@@ -85,16 +86,21 @@ func (m *StructMember) Print(indent string, output io.Writer) {
8586

8687
doc := formatDocs(true, indent, m.doc.Member(m.parent.Type, m.Member.Name), m.ovr.Comment)
8788

89+
extends := ""
90+
if len(m.Extends) > 0 {
91+
extends = strings.Join(m.Extends, ` & `) + ` & `
92+
}
93+
8894
if m.Members == nil {
89-
fmt.Fprintln(output, doc+indent+m.Name+optional+`: `+m.Type+`;`)
95+
fmt.Fprintln(output, doc+indent+m.Name+optional+`: `+extends+m.Type+`;`)
9096
return
9197
}
9298

9399
if optional = ""; m.Optional {
94100
optional = "null | "
95101
}
96102

97-
fmt.Fprintln(output, indent+m.Name+`: `+optional+`{`)
103+
fmt.Fprintln(output, indent+m.Name+`: `+optional+extends+`{`)
98104

99105
for _, m := range m.Members {
100106
m.Print(indent+` `, output)

printer_test.go

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,24 @@ import (
77
)
88

99
type TestWrapper struct {
10+
TestEndpoint
11+
1012
Profile TestLevel1
1113
Level1 TestLevel1
12-
TestEndpoint
13-
EP *TestEndpoint
14-
Auth struct {
14+
EP *TestEndpoint
15+
Auth struct {
16+
*TestEndpoint
17+
TestLevel1
18+
1519
Username string `json:"username"`
1620
Password string `json:"password"`
21+
Decoy struct {
22+
TestEndpoint
23+
*TestLevel1
24+
25+
Apple string
26+
Banana string
27+
}
1728
}
1829
Config *goty.Config
1930
}
@@ -68,22 +79,18 @@ func ExampleGoty_Print() {
6879
// Profile: TestLevel1;
6980
// Level1: TestLevel1;
7081
// EP?: TestEndpoint;
71-
// Auth: {
82+
// Auth: TestEndpoint & TestLevel1 & {
7283
// username: string;
7384
// password: string;
85+
// Decoy: TestEndpoint & TestLevel1 & {
86+
// Apple: string;
87+
// Banana: string;
88+
// };
7489
// };
7590
// Config?: Config;
7691
// };
7792
//
7893
// /**
79-
// * @see golang: <golift.io/goty_test.TestLevel1>
80-
// */
81-
// export interface TestLevel1 {
82-
// name: string;
83-
// date: Date;
84-
// };
85-
//
86-
// /**
8794
// * @see golang: <golift.io/goty_test.TestEndpoint>
8895
// */
8996
// export interface TestEndpoint {
@@ -92,6 +99,14 @@ func ExampleGoty_Print() {
9299
// };
93100
//
94101
// /**
102+
// * @see golang: <golift.io/goty_test.TestLevel1>
103+
// */
104+
// export interface TestLevel1 {
105+
// name: string;
106+
// date: Date;
107+
// };
108+
//
109+
// /**
95110
// * @see golang: <golift.io/goty.Config>
96111
// */
97112
// export interface Config {

0 commit comments

Comments
 (0)