-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from arran4/Custom
Custom output per type
- Loading branch information
Showing
8 changed files
with
229 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
*.txt | ||
*.pprof | ||
cmap2/ | ||
cache/ | ||
debug | ||
/*.txt | ||
/*.pprof | ||
/cmap2/ | ||
/cache/ | ||
/debug | ||
/testdata/testmodel1/s2ts_gen_*.go | ||
/helpers.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package struct2ts | ||
|
||
import "io" | ||
|
||
type tabScanner struct { | ||
output io.Writer | ||
tabs []byte | ||
} | ||
|
||
func (ts *tabScanner) Write(bs []byte) (n int, err error) { | ||
p := 0 | ||
var rn int | ||
for pos, b := range bs { | ||
switch b { | ||
case '\n': | ||
rn, err = ts.output.Write(bs[p : pos+1]) | ||
n += rn | ||
if err != nil { | ||
return | ||
} | ||
_, err = ts.output.Write(ts.tabs) | ||
if err != nil { | ||
return | ||
} | ||
p = pos + 1 | ||
} | ||
} | ||
rn, err = ts.output.Write(bs[p:]) | ||
return n + rn, err | ||
} | ||
|
||
func newTabScanner(w io.Writer, tabs string) io.Writer { | ||
return &tabScanner{ | ||
output: w, | ||
tabs: []byte(tabs), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package struct2ts | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
) | ||
|
||
func TestTabScanner_Write(t *testing.T) { | ||
tests := []struct { | ||
Name string | ||
Input struct { | ||
Tabs string | ||
Buffers []string | ||
} | ||
Expected struct { | ||
Output string | ||
n *int | ||
} | ||
}{ | ||
{ | ||
"One new line mid string - one input, 3 spaces", | ||
struct { | ||
Tabs string | ||
Buffers []string | ||
}{Tabs: " ", Buffers: []string{"Hello how are you?\nI'm fine thanks!"}}, | ||
struct { | ||
Output string | ||
n *int | ||
}{Output: "Hello how are you?\n I'm fine thanks!"}, | ||
}, | ||
{ | ||
"One new line end string - one input, 3 spaces", | ||
struct { | ||
Tabs string | ||
Buffers []string | ||
}{Tabs: " ", Buffers: []string{"Hello how are you?\n"}}, | ||
struct { | ||
Output string | ||
n *int | ||
}{Output: "Hello how are you?\n "}, | ||
}, | ||
{ | ||
"One new line start string - one input, 3 spaces", | ||
struct { | ||
Tabs string | ||
Buffers []string | ||
}{Tabs: " ", Buffers: []string{"\nHello how are you?"}}, | ||
struct { | ||
Output string | ||
n *int | ||
}{Output: "\n Hello how are you?"}, | ||
}, | ||
{ | ||
"empty string", | ||
struct { | ||
Tabs string | ||
Buffers []string | ||
}{Tabs: " ", Buffers: []string{""}}, | ||
struct { | ||
Output string | ||
n *int | ||
}{Output: ""}, | ||
}, | ||
{ | ||
"Just a new line", | ||
struct { | ||
Tabs string | ||
Buffers []string | ||
}{Tabs: " ", Buffers: []string{"\n"}}, | ||
struct { | ||
Output string | ||
n *int | ||
}{Output: "\n "}, | ||
}, | ||
} | ||
for _, test := range tests { | ||
t.Run(test.Name, func(t *testing.T) { | ||
outputBuffer := bytes.NewBuffer(nil) | ||
ts := newTabScanner(outputBuffer, test.Input.Tabs) | ||
for _, s := range test.Input.Buffers { | ||
_, err := ts.Write([]byte(s)) | ||
if err != nil { | ||
t.Error("Buffer write error", err) | ||
} | ||
} | ||
if outputBuffer.String() != test.Expected.Output { | ||
t.Log("Output buffer doesn't match. Got ") | ||
t.Log(outputBuffer.String()) | ||
t.Log("expected") | ||
t.Log(test.Expected.Output) | ||
t.Fail() | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package testmodel1 | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
) | ||
|
||
type Struct1 struct { | ||
HasNoCustomInterface int | ||
} | ||
|
||
type Struct2 struct { | ||
TotallyDoesHaveOneAsAPointer int | ||
} | ||
|
||
func (s *Struct2) RenderCustomTypescript(w io.Writer) (err error) { | ||
fmt.Fprint(w, "// Custom Output!!!") | ||
return nil | ||
} | ||
|
||
type Struct3 struct { | ||
TotallyDoesHaveOne int | ||
} | ||
|
||
func (s Struct3) RenderCustomTypescript(w io.Writer) (err error) { | ||
fmt.Fprint(w, "// Custom Output!!!") | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#!/bin/bash -Xe | ||
|
||
go run github.com/OneOfOne/struct2ts/cmd/struct2ts github.com/OneOfOne/struct2ts/testdata/testmodel1.Struct1 | ||
go run github.com/OneOfOne/struct2ts/cmd/struct2ts github.com/OneOfOne/struct2ts/testdata/testmodel1.Struct2 | ||
go run github.com/OneOfOne/struct2ts/cmd/struct2ts github.com/OneOfOne/struct2ts/testdata/testmodel1.Struct3 |