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

adds extraction type mapping option #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
15 changes: 14 additions & 1 deletion extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ type extractor struct {
docHandler DocHandler

result map[string]TypescriptType

customTypeMapping map[reflect.Kind]*TypescriptType
}

// EmbedStructs produces a single monolithic structure where all
Expand Down Expand Up @@ -82,6 +84,12 @@ func WithDocumentation(handler DocHandler) ExtractOption {
}
}

func TypeMapping(kind reflect.Kind, ty *TypescriptType) ExtractOption {
return func(e *extractor) {
e.customTypeMapping[kind] = ty
}
}

// SortAlphabetically sorts all types and their members alphabetically
func SortAlphabetically(e *extractor) {
sorter := func(a, b interface{}) bool {
Expand Down Expand Up @@ -121,7 +129,8 @@ func Extract(s interface{}, opts ...ExtractOption) ([]TypescriptType, error) {
typeNamer: func(t reflect.Type) string {
return strcase.ToCamel(t.Name())
},
docHandler: (*nullDocHandler)(nil),
docHandler: (*nullDocHandler)(nil),
customTypeMapping: map[reflect.Kind]*TypescriptType{},
}
for _, opt := range opts {
opt(e)
Expand Down Expand Up @@ -374,6 +383,10 @@ func (e *extractor) getPrimitiveType(t reflect.Type) (*TypescriptType, error) {
}

kind := t.Kind()
if mapping, ok := e.customTypeMapping[kind]; ok {
return mapping, nil
}

switch kind {
case reflect.Bool:
return mktype("boolean"), nil
Expand Down