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 custom marshaller and custom output file #589

Open
wants to merge 2 commits 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
38 changes: 31 additions & 7 deletions entoas/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ type (
mutations []MutateFunc
out io.Writer
spec *ogen.Spec
marshalFn func(any) ([]byte, error)
outFile string
}
// ExtensionOption allows managing Extension configuration using functional arguments.
ExtensionOption func(*Extension) error
Expand All @@ -69,11 +71,17 @@ type (

// NewExtension returns a new entoas extension with default values.
func NewExtension(opts ...ExtensionOption) (*Extension, error) {
ex := &Extension{config: &Config{
DefaultPolicy: PolicyExpose,
MinItemsPerPage: one,
MaxItemsPerPage: maxu8,
}}
ex := &Extension{
marshalFn: func(v any) ([]byte, error) {
return json.MarshalIndent(v, "", " ")
},
outFile: "openapi.json",
config: &Config{
DefaultPolicy: PolicyExpose,
MinItemsPerPage: one,
MaxItemsPerPage: maxu8,
},
}
for _, opt := range opts {
if err := opt(ex); err != nil {
return nil, err
Expand Down Expand Up @@ -155,6 +163,22 @@ func WriteTo(out io.Writer) ExtensionOption {
}
}

// WithOutputFile specifies the file to write generated spec to.
func WithOutputFile(outFile string) ExtensionOption {
return func(ex *Extension) error {
ex.outFile = outFile
return nil
}
}

// WithMarshalFunc sets the marshaller to marshal spec.
func WithMarshalFunc(marshalFn func(any) ([]byte, error)) ExtensionOption {
return func(ex *Extension) error {
ex.marshalFn = marshalFn
return nil
}
}

// Spec allows to configure a pointer to an existing ogen.Spec where the code generator writes the final result to.
// Any configured Mutations are run before the spec is written.
func Spec(spec *ogen.Spec) ExtensionOption {
Expand Down Expand Up @@ -204,7 +228,7 @@ func (ex *Extension) generate(next gen.Generator) gen.Generator {
*ex.spec = *spec
}
// Dump the spec.
b, err := json.MarshalIndent(spec, "", " ")
b, err := ex.marshalFn(spec)
if err != nil {
return err
}
Expand All @@ -213,7 +237,7 @@ func (ex *Extension) generate(next gen.Generator) gen.Generator {
_, err = ex.out.Write(b)
return err
}
return os.WriteFile(filepath.Join(g.Target, "openapi.json"), b, 0644)
return os.WriteFile(filepath.Join(g.Target, ex.outFile), b, 0644)
})
}

Expand Down