-
Notifications
You must be signed in to change notification settings - Fork 136
Support marshal with linebreak and indent and support ignore specific struct field #150
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
Open
funte
wants to merge
7
commits into
invopop:main
Choose a base branch
from
funte:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7f1f647
Support marshal with linebreak and indent
funte bfefafd
Support ignore specific struct field
funte 157ac74
Update go.mod
funte 6164201
Merge pull request #158 from invopop/codecov
samlown 4b9b780
Merge pull request #159 from misberner/mi/custom-comment-lookup
samlown c7e4988
Support manual require
funte 44e8c5e
Update README.md
funte File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -122,6 +122,9 @@ type Reflector struct { | |
| // switching to just allowing additional properties instead. | ||
| IgnoredTypes []any | ||
|
|
||
| // Ignore specific struct field, enable dynamic generate different schemas from on struct. | ||
| Ignore func(reflect.StructField) bool | ||
|
|
||
| // Lookup allows a function to be defined that will provide a custom mapping of | ||
| // types to Schema IDs. This allows existing schema documents to be referenced | ||
| // by their ID instead of being embedded into the current schema definitions. | ||
|
|
@@ -492,6 +495,10 @@ func (r *Reflector) reflectStructFields(st *Schema, definitions Definitions, t r | |
| } | ||
|
|
||
| handleField := func(f reflect.StructField) { | ||
| if !f.Anonymous && r.Ignore != nil && r.Ignore(f) { | ||
| return | ||
| } | ||
|
|
||
| name, shouldEmbed, required, nullable := r.reflectFieldName(f) | ||
| // if anonymous and exported type should be processed recursively | ||
| // current type should inherit properties of anonymous one | ||
|
|
@@ -1081,6 +1088,11 @@ func (t *Schema) UnmarshalJSON(data []byte) error { | |
| return json.Unmarshal(data, aux) | ||
| } | ||
|
|
||
| // If true, marshal json with linebreak and indent. | ||
| var MarshalWithIndent = false | ||
| var MarshalPrefix = "" | ||
| var MarshalIndent = "\t" | ||
|
Comment on lines
+1097
to
+1099
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Global variables should be avoided. Wouldn't you want to do this with the regular |
||
|
|
||
| // MarshalJSON is used to serialize a schema object or boolean. | ||
| func (t *Schema) MarshalJSON() ([]byte, error) { | ||
| if t.boolean != nil { | ||
|
|
@@ -1094,14 +1106,25 @@ func (t *Schema) MarshalJSON() ([]byte, error) { | |
| return []byte("true"), nil | ||
| } | ||
| type SchemaAlt Schema | ||
| b, err := json.Marshal((*SchemaAlt)(t)) | ||
| var b []byte | ||
| var err error | ||
| if MarshalWithIndent { | ||
| b, err = json.MarshalIndent((*SchemaAlt)(t), MarshalPrefix, MarshalIndent) | ||
| } else { | ||
| b, err = json.Marshal((*SchemaAlt)(t)) | ||
| } | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| if t.Extras == nil || len(t.Extras) == 0 { | ||
| return b, nil | ||
| } | ||
| m, err := json.Marshal(t.Extras) | ||
| var m []byte | ||
| if MarshalWithIndent { | ||
| m, err = json.MarshalIndent(t.Extras, MarshalPrefix, MarshalIndent) | ||
| } else { | ||
| m, err = json.Marshal(t.Extras) | ||
| } | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Its not clear how this should be used... as a rule, I'd try to avoid using reflect types directly.