From 8e50e99a73dd7fe1031080beefa750421d2b069b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Thanh=20Quang?= <57827456+AlphaNecron@users.noreply.github.com> Date: Thu, 27 Jun 2024 01:19:19 +0700 Subject: [PATCH 1/2] add support for custom marshaller and custom output file This PR adds support for specifying custom `Marshaller` and output file when initializing `entoas` extension. Sadly, only `github.com/go-faster/yaml` and `encoding/json` seem to work up to now. --- entoas/extension.go | 38 +++++++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/entoas/extension.go b/entoas/extension.go index 5c155ec3b..2f6d6a642 100644 --- a/entoas/extension.go +++ b/entoas/extension.go @@ -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 @@ -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 @@ -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 { @@ -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 } @@ -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) }) } From 3327600e4b0804a56132898705be650fc3b758b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Thanh=20Quang?= <57827456+AlphaNecron@users.noreply.github.com> Date: Thu, 27 Jun 2024 11:56:14 +0700 Subject: [PATCH 2/2] fix default marshaller's indent --- entoas/extension.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/entoas/extension.go b/entoas/extension.go index 2f6d6a642..386f5e57f 100644 --- a/entoas/extension.go +++ b/entoas/extension.go @@ -73,7 +73,7 @@ type ( func NewExtension(opts ...ExtensionOption) (*Extension, error) { ex := &Extension{ marshalFn: func(v any) ([]byte, error) { - return json.MarshalIndent(v, "", " ") + return json.MarshalIndent(v, "", " ") }, outFile: "openapi.json", config: &Config{