Skip to content

Commit

Permalink
minor enums and set fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
vitorsss committed Sep 3, 2024
1 parent 4f0fafd commit a587167
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 32 deletions.
13 changes: 8 additions & 5 deletions pkg/parsers/edntostruct/contentparser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ package path
import (
errors "errors"
fmt "fmt"
strings "strings"
edn "olympos.io/encoding/edn"
)
Expand Down Expand Up @@ -201,7 +203,7 @@ func (e KeywordEnumNamesssEnum2Code) MarshalEDN() ([]byte, error) {
args: &args{
packagePath: "example.com/some/package/path",
prefix: "SetsSetsSets",
ednContent: []byte(`{:superset/ok #{{:subtype/id 2 :subtype/name "ttt"}} :superset/string #{"some_string"} :superset/keyword #{:some_keyword} :superset/int #{11 33}}`),
ednContent: []byte(`{:superset/ok #{{:subtype/id 2 :subtype/name "ttt"}} :superset/string #{"some_string"} :superset/keyword #{:some_keyword} :superset/int #{11 33} :superset/set-of-set #{#{"why" "just" "why?"}}}`),
},
want: &want{
result: `// Code generated by endtostruct DO NOT EDIT
Expand All @@ -213,10 +215,11 @@ import (
)
type SetsSetsSetsSuperset struct {
Int entity.Set[int64 /* type parameter */] "json:\"int\" edn:\"superset/int\""
Keyword entity.Set[SetsSetsSetsSupersetKeywordCode] "json:\"keyword\" edn:\"superset/keyword\""
Ok entity.Set[SetsSetsSetsSupersetSubtype] "json:\"ok\" edn:\"superset/ok\""
String entity.Set[string /* type parameter */] "json:\"string\" edn:\"superset/string\""
Int entity.Set[int64] "json:\"int\" edn:\"superset/int\""
Keyword entity.Set[SetsSetsSetsSupersetKeywordCode] "json:\"keyword\" edn:\"superset/keyword\""
Ok entity.Set[SetsSetsSetsSupersetSubtype] "json:\"ok\" edn:\"superset/ok\""
SetOfSet entity.Set[entity.Set[string]] "json:\"set_of_set\" edn:\"superset/set-of-set\""
String entity.Set[string] "json:\"string\" edn:\"superset/string\""
}
type SetsSetsSetsSupersetKeywordCode string
Expand Down
3 changes: 3 additions & 0 deletions pkg/parsers/edntostruct/enumtype.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ func createEnumOrderedValues(
name string,
values []string,
) *types.Named {
slices.Sort(values)
enumType := &enumType{
namespace: namespace,
name: name,
Expand Down Expand Up @@ -127,6 +128,8 @@ func newEnumType(
addImportFixName(destPackage, EDNPackage)
if namespace != "" {
addImportFixName(destPackage, ErrorsPackage)
addImportFixName(destPackage, StringsPackage)
addImportFixName(destPackage, FormatPackage)
}

return object, nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/parsers/edntostruct/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func printPackage(
destPackage *types.Package,
) ([]byte, error) {
qualifier := func(other *types.Package) string {
if destPackage == other {
if destPackage.Path() == other.Path() {
return ""
}
return other.Name()
Expand Down
30 changes: 9 additions & 21 deletions pkg/parsers/edntostruct/settype.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,24 @@ package edntostruct
import (
"go/token"
"go/types"

"github.com/pkg/errors"
)

var setTypeName = types.NewTypeName(
token.NoPos,
EntityPackage,
"Set",
nil,
)

func newSetType(
destPackage *types.Package,
elem types.Type,
) (types.Type, error) {
var typeName *types.TypeName
switch v := elem.(type) {
case *types.Named:
typeName = v.Obj()
case *types.Basic:
typeName = types.Universe.Lookup(v.String()).(*types.TypeName)
default:
return nil, errors.New("unmapped set type")
}
typeParam := types.NewTypeParam(
typeName,
nil,
types.NewTypeName(token.NoPos, EntityPackage, "", nil),
types.NewInterfaceType(nil, nil),
)
typeParam.SetConstraint(elem)
named := types.NewNamed(
setTypeName,
types.NewTypeName(
token.NoPos,
EntityPackage,
"Set",
nil,
),
types.NewSlice(elem),
nil,
)
Expand Down
12 changes: 7 additions & 5 deletions pkg/parsers/edntostruct/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ func genericTypeFn(t types.Type) TypeFn {
}

var (
EDNPackage = types.NewPackage("olympos.io/encoding/edn", "edn")
EntityPackage = types.NewPackage("github.com/vitorsss/go-helpers/pkg/entity", "entity")
ErrorsPackage = types.NewPackage("errors", "errors")
TimePackage = types.NewPackage("time", "time")
UUIDPackage = types.NewPackage("github.com/google/uuid", "uuid")
EDNPackage = types.NewPackage("olympos.io/encoding/edn", "edn")
EntityPackage = types.NewPackage("github.com/vitorsss/go-helpers/pkg/entity", "entity")
ErrorsPackage = types.NewPackage("errors", "errors")
FormatPackage = types.NewPackage("fmt", "fmt")
StringsPackage = types.NewPackage("strings", "strings")
TimePackage = types.NewPackage("time", "time")
UUIDPackage = types.NewPackage("github.com/google/uuid", "uuid")

UUIDType = types.NewNamed(
types.NewTypeName(
Expand Down

0 comments on commit a587167

Please sign in to comment.