Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for direct string translation, add conversion tests #70

Merged
merged 5 commits into from
Sep 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Test Tygo
on:
pull_request:
push:

jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
go-version:
- 1.22.x
steps:
- name: checkout
uses: actions/checkout@v4
- name: install
uses: actions/setup-go@v5
with:
go-version: '${{ matrix.go-version }}'
- name: vet
run: go vet ./...
- name: test
run: go test -v -race ./...
14 changes: 0 additions & 14 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ import (
"gopkg.in/yaml.v2"
)

const defaultFallbackType = "any"
const defaultPreserveComments = "default"

func ReadFromFilepath(cfgFilepath string) tygo.Config {
b, err := ioutil.ReadFile(cfgFilepath)
if err != nil {
Expand All @@ -22,16 +19,5 @@ func ReadFromFilepath(cfgFilepath string) tygo.Config {
log.Fatalf("Could not parse config file from: %v", err)
}

// apply defaults
for _, packageConf := range conf.Packages {
if packageConf.FallbackType == "" {
packageConf.FallbackType = defaultFallbackType
}

if packageConf.PreserveComments == "" {
packageConf.PreserveComments = defaultPreserveComments
}
}

return conf
}
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,20 @@ require (
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/mod v0.5.1 // indirect
golang.org/x/sys v0.0.0-20211205182925-97ca703d548d // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

require (
github.com/fatih/structtag v1.2.0
github.com/google/uuid v1.3.0
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/testify v1.9.0
golang.org/x/tools v0.1.9
gopkg.in/guregu/null.v4 v4.0.0
)
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ github.com/coreos/go-semver v0.3.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3Ee
github.com/coreos/go-systemd/v22 v22.3.2/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
Expand Down Expand Up @@ -281,6 +282,7 @@ github.com/pelletier/go-toml v1.9.4/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCko
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/sftp v1.10.1/go.mod h1:lYOWFsE0bwd1+KfKJaKeuokY15vzFx25BLbzYYoAxZI=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI=
github.com/posener/complete v1.2.3/go.mod h1:WZIdtGGp+qx0sLrYKtIRAruyNpv6hFCicSgv7Sy7s/s=
Expand Down Expand Up @@ -322,6 +324,8 @@ github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81P
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
github.com/tv42/httpunix v0.0.0-20150427012821-b75d8614f926/go.mod h1:9ESjWnEqriFuLhtthL60Sar/7RFoluCcXsuvEwTV5KM=
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down Expand Up @@ -762,6 +766,8 @@ gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand Down
56 changes: 38 additions & 18 deletions tygo/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
)

const defaultOutputFilename = "index.ts"
const defaultFallbackType = "any"
const defaultPreserveComments = "default"

type PackageConfig struct {
// The package path just like you would import it in Go
Expand Down Expand Up @@ -77,27 +79,12 @@ func (c Config) PackageNames() []string {
func (c Config) PackageConfig(packagePath string) *PackageConfig {
for _, pc := range c.Packages {
if pc.Path == packagePath {
if pc.Indent == "" {
pc.Indent = " "
}

var err error
pc.Flavor, err = normalizeFlavor(pc.Flavor)
if err != nil {
log.Fatalf("Invalid config for package %s: %s", packagePath, err)
}

pc.PreserveComments, err = normalizePreserveComments(pc.PreserveComments)
pcNormalized, err := pc.Normalize()
if err != nil {
log.Fatalf("Invalid config for package %s: %s", packagePath, err)
log.Fatalf("Error in config for package %s: %s", packagePath, err)
}

pc.OptionalType, err = normalizeOptionalType(pc.OptionalType)
if err != nil {
log.Fatalf("Invalid config for package %s: %s", packagePath, err)
}

return pc
return &pcNormalized
}
}
log.Fatalf("Config not found for package %s", packagePath)
Expand Down Expand Up @@ -168,3 +155,36 @@ func (c PackageConfig) ResolvedOutputPath(packageDir string) string {
}
return c.OutputPath
}

// Normalize returns a new PackageConfig with default values set.
func (pc PackageConfig) Normalize() (PackageConfig, error) {
if pc.Indent == "" {
pc.Indent = " "
}

if pc.FallbackType == "" {
pc.FallbackType = defaultFallbackType
}

if pc.PreserveComments == "" {
pc.PreserveComments = defaultPreserveComments
}

var err error
pc.Flavor, err = normalizeFlavor(pc.Flavor)
if err != nil {
return pc, fmt.Errorf("invalid flavor config for package %s: %s", pc.Path, err)
}

pc.PreserveComments, err = normalizePreserveComments(pc.PreserveComments)
if err != nil {
return pc, fmt.Errorf("invalid preserve_comments config for package %s: %s", pc.Path, err)
}

pc.OptionalType, err = normalizeOptionalType(pc.OptionalType)
if err != nil {
return pc, fmt.Errorf("invalid optional_type config for package %s: %s", pc.Path, err)
}

return pc, nil
}
41 changes: 41 additions & 0 deletions tygo/convert_string.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package tygo

import (
"fmt"
"go/parser"
"go/token"
"strings"
)

// ConvertGoToTypescript converts Go code string to Typescript.
//
// This is mostly useful for testing purposes inside tygo itself.
func ConvertGoToTypescript(goCode string, pkgConfig PackageConfig) (string, error) {
src := fmt.Sprintf(`package tygoconvert

%s`, goCode)

fset := token.NewFileSet()

f, err := parser.ParseFile(fset, "", src, parser.AllErrors|parser.ParseComments)
if err != nil {
return "", fmt.Errorf("failed to parse source: %w", err)
}

pkgConfig, err = pkgConfig.Normalize()
if err != nil {
return "", fmt.Errorf("failed to normalize package config: %w", err)
}

pkgGen := &PackageGenerator{
conf: &pkgConfig,
pkg: nil,
}

s := new(strings.Builder)

pkgGen.generateFile(s, f, "")
code := s.String()

return code, nil
}
113 changes: 113 additions & 0 deletions tygo/fixtures_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package tygo

import (
"embed"
"fmt"
"strings"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
)

// Embed markdown test fixtures
//
//go:embed testdata/fixtures/*.md
var mdfs embed.FS

type MarkdownFixture struct {
PackageConfig PackageConfig
GoCode string
TsCode string
}

func TestConvertGoToTypescriptSmoketest(t *testing.T) {
t.Parallel()

goCode := "type MyType uint8"
tsCode, err := ConvertGoToTypescript(goCode, PackageConfig{})
require.NoError(t, err)

expected := `export type MyType = number /* uint8 */;
`
assert.Equal(t, expected, tsCode)
}

func parseMarkdownFixtures(fileContents []byte) ([]MarkdownFixture, error) {
fixtures := make([]MarkdownFixture, 0)
currentFixture := MarkdownFixture{}

currentBlockContents := ""
currentBlockLanguage := ""
inCodeBlock := false
for _, line := range strings.Split(string(fileContents), "\n") {
if strings.HasPrefix(line, "```") {
if inCodeBlock {
// End of code block
if currentBlockLanguage == "ts" || currentBlockLanguage == "typescript" {
// Every fixture ends with a typescript block
currentFixture.TsCode = currentBlockContents
fixtures = append(fixtures, currentFixture)
currentFixture = MarkdownFixture{}
} else if currentBlockLanguage == "go" {
currentFixture.GoCode = currentBlockContents
} else if currentBlockLanguage == "yml" || currentBlockLanguage == "yaml" {
// Parse package config
pc := PackageConfig{}
err := yaml.Unmarshal([]byte(currentBlockContents), &pc)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal package config: %w", err)
}
currentFixture.PackageConfig = pc
}
currentBlockContents = ""
currentBlockLanguage = ""
} else { // Start of code block
language := strings.TrimPrefix(line, "```")
language = strings.TrimSpace(language)
currentBlockLanguage = language
}
inCodeBlock = !inCodeBlock
continue
}

if inCodeBlock {
currentBlockContents += line + "\n"
}
}

return fixtures, nil

}

// Tests all markdown files in `testdata/fixtures/` directory.
func TestMarkdownFixtures(t *testing.T) {
t.Parallel()

fixtures, err := mdfs.ReadDir("testdata/fixtures")
require.NoError(t, err)

for _, fixture := range fixtures {
fixture := fixture

// Read markdown file
md, err := mdfs.ReadFile("testdata/fixtures/" + fixture.Name())
require.NoError(t, err)

testCases, err := parseMarkdownFixtures(md)
require.NoError(t, err)

for _, tc := range testCases {
tc := tc
t.Run(fixture.Name(), func(t *testing.T) {
t.Parallel()

tsCode, err := ConvertGoToTypescript(tc.GoCode, tc.PackageConfig)
require.NoError(t, err)

assert.Equal(t, tc.TsCode, tsCode)
})
}
}
}
3 changes: 1 addition & 2 deletions tygo/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package tygo

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"

Expand Down Expand Up @@ -73,7 +72,7 @@ func (g *Tygo) Generate() error {
return nil
}

err = ioutil.WriteFile(outPath, []byte(code), 0664)
err = os.WriteFile(outPath, []byte(code), 0664)
if err != nil {
return nil
}
Expand Down
Loading