Skip to content
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
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ err := gen.Generate()
## Config

```yaml
# You can specify default mappings that will apply to all packages.
type_mappings:
time.Time: "string /* RFC3339 */"

# You can specify more than one package
packages:
# The package path just like you would import it in Go
Expand All @@ -149,7 +153,8 @@ packages:
indent: " "

# Specify your own custom type translations, useful for custom types, `time.Time` and `null.String`.
# Be default unrecognized types will be `any`.
# By default unrecognized types will be `any`.
# A mapping specified here will override one specified globally.
type_mappings:
time.Time: "string"
my.Type: "SomeType"
Expand Down
7 changes: 7 additions & 0 deletions examples/globalconfig/globalconfig.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package globalconfig

import "time"

type Config struct {
Duration time.Duration
}
4 changes: 4 additions & 0 deletions tygo.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# This is a config file that typescriptifies the packages under the example folder.
# and some other packages.
type_mappings:
time.Duration: "number /* int, ns */"

packages:
- path: "github.com/gzuidhof/tygo/examples/bookstore"
Expand Down Expand Up @@ -56,4 +58,6 @@ packages:
- "excluded.go"
- path: "github.com/gzuidhof/tygo/examples/rune"

- path: "github.com/gzuidhof/tygo/examples/globalconfig"


15 changes: 14 additions & 1 deletion tygo/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ type PackageConfig struct {
}

type Config struct {
Packages []*PackageConfig `yaml:"packages"`
TypeMappings map[string]string `yaml:"type_mappings"`
Packages []*PackageConfig `yaml:"packages"`
}

func (c Config) PackageNames() []string {
Expand All @@ -79,6 +80,7 @@ func (c Config) PackageNames() []string {
func (c Config) PackageConfig(packagePath string) *PackageConfig {
for _, pc := range c.Packages {
if pc.Path == packagePath {
pc.TypeMappings = c.mergeMappings(pc.TypeMappings)
pcNormalized, err := pc.Normalize()
if err != nil {
log.Fatalf("Error in config for package %s: %s", packagePath, err)
Expand Down Expand Up @@ -188,3 +190,14 @@ func (pc PackageConfig) Normalize() (PackageConfig, error) {

return pc, nil
}

func (c Config) mergeMappings(pkg map[string]string) map[string]string {
mappings := make(map[string]string)
for k, v := range c.TypeMappings {
mappings[k] = v
}
for k, v := range pkg {
mappings[k] = v
}
return mappings
}
Loading