-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #731 from tdakkota/feat/empty-type-custom-format
feat(gen): allow to use empty interface as custom format type
- Loading branch information
Showing
21 changed files
with
640 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Package eventtype defines a custom format for event types. | ||
package eventtype | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"github.com/go-faster/jx" | ||
|
||
"github.com/ogen-go/ogen/gen" | ||
) | ||
|
||
type Event = any | ||
|
||
// EventFormat defines a custom format for Event. | ||
var EventFormat = gen.CustomFormat[ | ||
Event, | ||
JSONEventEncoding, | ||
TextEventEncoding, | ||
]() | ||
|
||
// JSONEventEncoding defines a custom JSON encoding for hexadecimal numbers. | ||
type JSONEventEncoding struct{} | ||
|
||
// EncodeJSON encodes a hexadecimal number as a JSON string. | ||
func (JSONEventEncoding) EncodeJSON(e *jx.Encoder, v Event) { | ||
b, err := json.Marshal(v) | ||
if err != nil { | ||
e.Null() | ||
return | ||
} | ||
e.Raw(b) | ||
} | ||
|
||
// DecodeJSON decodes a hexadecimal number from a JSON string. | ||
func (JSONEventEncoding) DecodeJSON(d *jx.Decoder) (v Event, _ error) { | ||
r, err := d.Raw() | ||
if err != nil { | ||
return v, err | ||
} | ||
err = json.Unmarshal(r, &v) | ||
return v, err | ||
} | ||
|
||
// TextEventEncoding defines a custom text encoding for hexadecimal numbers. | ||
type TextEventEncoding struct{} | ||
|
||
// EncodeText encodes a hexadecimal number as a string. | ||
func (TextEventEncoding) EncodeText(v Event) string { | ||
b, err := json.Marshal(v) | ||
if err != nil { | ||
return "" | ||
} | ||
return string(b) | ||
} | ||
|
||
// DecodeText decodes a hexadecimal number from a string. | ||
func (TextEventEncoding) DecodeText(s string) (v Event, _ error) { | ||
err := json.Unmarshal([]byte(s), &v) | ||
return v, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.