Skip to content

feat: add templates #1

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

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
86 changes: 4 additions & 82 deletions enumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import "fmt"

// Arguments to format are:
//
// [1]: type name
const stringNameToValueMethod = `// %[1]sString retrieves an enum value from the enum constants string name.
// Throws an error if the param is not part of the enum.
Expand All @@ -15,6 +16,7 @@ func %[1]sString(s string) (%[1]s, error) {
`

// Arguments to format are:
//
// [1]: type name
const stringValuesMethod = `// %[1]sValues returns all values of the enum
func %[1]sValues() []%[1]s {
Expand All @@ -23,6 +25,7 @@ func %[1]sValues() []%[1]s {
`

// Arguments to format are:
//
// [1]: type name
const stringBelongsMethodLoop = `// IsA%[1]s returns "true" if the value is listed in the enum definition. "false" otherwise
func (i %[1]s) IsA%[1]s() bool {
Expand All @@ -36,6 +39,7 @@ func (i %[1]s) IsA%[1]s() bool {
`

// Arguments to format are:
//
// [1]: type name
const stringBelongsMethodSet = `// IsA%[1]s returns "true" if the value is listed in the enum definition. "false" otherwise
func (i %[1]s) IsA%[1]s() bool {
Expand Down Expand Up @@ -86,85 +90,3 @@ func (g *Generator) buildBasicExtras(runs [][]Value, typeName string, runsThresh
g.Printf(stringBelongsMethodSet, typeName)
}
}

// Arguments to format are:
// [1]: type name
const enumValuesMethod = `// EnumValues returns an array of the values of this type
func (i %[1]s) EnumValues() []%[1]s {
return _%[1]sValues
}
`

func (g *Generator) buildEnumValues(runs [][]Value, typeName string, runsThreshold int) {
g.Printf(enumValuesMethod, typeName)
}

// Arguments to format are:
// [1]: type name
const jsonMethods = `
// MarshalJSON implements the json.Marshaler interface for %[1]s
func (i %[1]s) MarshalJSON() ([]byte, error) {
return json.Marshal(i.String())
}

// UnmarshalJSON implements the json.Unmarshaler interface for %[1]s
func (i *%[1]s) UnmarshalJSON(data []byte) error {
var s string
if err := json.Unmarshal(data, &s); err != nil {
return fmt.Errorf("%[1]s should be a string, got %%s", data)
}

var err error
*i, err = %[1]sString(s)
return err
}
`

func (g *Generator) buildJSONMethods(runs [][]Value, typeName string, runsThreshold int) {
g.Printf(jsonMethods, typeName)
}

// Arguments to format are:
// [1]: type name
const textMethods = `
// MarshalText implements the encoding.TextMarshaler interface for %[1]s
func (i %[1]s) MarshalText() ([]byte, error) {
return []byte(i.String()), nil
}

// UnmarshalText implements the encoding.TextUnmarshaler interface for %[1]s
func (i *%[1]s) UnmarshalText(text []byte) error {
var err error
*i, err = %[1]sString(string(text))
return err
}
`

func (g *Generator) buildTextMethods(runs [][]Value, typeName string, runsThreshold int) {
g.Printf(textMethods, typeName)
}

// Arguments to format are:
// [1]: type name
const yamlMethods = `
// MarshalYAML implements a YAML Marshaler for %[1]s
func (i %[1]s) MarshalYAML() (interface{}, error) {
return i.String(), nil
}

// UnmarshalYAML implements a YAML Unmarshaler for %[1]s
func (i *%[1]s) UnmarshalYAML(unmarshal func(interface{}) error) error {
var s string
if err := unmarshal(&s); err != nil {
return err
}

var err error
*i, err = %[1]sString(s)
return err
}
`

func (g *Generator) buildYAMLMethods(runs [][]Value, typeName string, runsThreshold int) {
g.Printf(yamlMethods, typeName)
}
5 changes: 2 additions & 3 deletions golden_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
package main

import (
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -1142,7 +1141,7 @@ func runGoldenTest(t *testing.T, test Golden, generateJSON, generateYAML, genera
input := "package test\n" + test.input
file := test.name + ".go"

dir, err := ioutil.TempDir("", "stringer")
dir, err := os.MkdirTemp("", "stringer")
if err != nil {
t.Error(err)
}
Expand All @@ -1154,7 +1153,7 @@ func runGoldenTest(t *testing.T, test Golden, generateJSON, generateYAML, genera
}()

absFile := filepath.Join(dir, file)
err = ioutil.WriteFile(absFile, []byte(input), 0644)
err = os.WriteFile(absFile, []byte(input), 0644)
if err != nil {
t.Error(err)
}
Expand Down
40 changes: 0 additions & 40 deletions sql.go

This file was deleted.

45 changes: 0 additions & 45 deletions sql_int.go

This file was deleted.

Loading