-
Notifications
You must be signed in to change notification settings - Fork 40
feat: union schema #228
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
Merged
Merged
feat: union schema #228
Changes from 10 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
906fc46
wp
Oudwins 0da04f3
wp
Oudwins 9cc7846
wp
Oudwins def7d44
chore
Oudwins 658555f
fix: typo
Oudwins 09a0c51
test: validate tests
Oudwins ba507b2
test: string + struct
Oudwins a4ff802
test: fix
Oudwins 0fa94d3
test: added parse tests
Oudwins 6288d84
feat: add to zss
Oudwins 02f87f4
fix: branch transforms impacting other unions
Oudwins e6e0bcb
wp
Oudwins 61bd1ee
wp
Oudwins 16cd802
wp
Oudwins 33e64c5
wp
Oudwins 09ef142
wp
Oudwins fcdde89
wp
Oudwins a5b3d04
fix: review
Oudwins 1c3a152
fix: remove min 2 requirement for union for consistency
Oudwins 007524e
fix: review
Oudwins 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
Some comments aren't visible on the classic Files Changed page.
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
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
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
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
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
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 |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| package zog | ||
|
|
||
| import ( | ||
| "github.com/Oudwins/zog/conf" | ||
| p "github.com/Oudwins/zog/pkgs/internals" | ||
| zss "github.com/Oudwins/zog/pkgs/zss/core" | ||
| "github.com/Oudwins/zog/zconst" | ||
| ) | ||
|
|
||
| var _ ZogSchema = &UnionSchema{} | ||
|
|
||
| type UnionSchema struct { | ||
| schemas []ZogSchema | ||
| } | ||
|
|
||
| func Union(schemas []ZogSchema, options ...SchemaOption) *UnionSchema { | ||
| s := &UnionSchema{ | ||
| schemas: schemas, | ||
| } | ||
| for _, opt := range options { | ||
| opt(s) | ||
| } | ||
| return s | ||
| } | ||
|
|
||
| func (u *UnionSchema) Parse(data any, dest any, options ...ExecOption) p.ZogIssueList { | ||
| errs := p.NewErrsList() | ||
| defer errs.Free() | ||
| ctx := p.NewExecCtx(errs, conf.IssueFormatter) | ||
| defer ctx.Free() | ||
| for _, opt := range options { | ||
| opt(ctx) | ||
| } | ||
| path := p.NewPathBuilder() | ||
| defer path.Free() | ||
| sctx := ctx.NewSchemaCtx(data, dest, path, u.getType()) | ||
| defer sctx.Free() | ||
| u.process(sctx) | ||
| return errs.List | ||
| } | ||
|
|
||
| func (u *UnionSchema) Validate(dest any, options ...ExecOption) p.ZogIssueList { | ||
| errs := p.NewErrsList() | ||
| defer errs.Free() | ||
| ctx := p.NewExecCtx(errs, conf.IssueFormatter) | ||
| defer ctx.Free() | ||
| for _, opt := range options { | ||
| opt(ctx) | ||
| } | ||
| path := p.NewPathBuilder() | ||
| defer path.Free() | ||
| sctx := ctx.NewSchemaCtx(dest, dest, path, u.getType()) | ||
| defer sctx.Free() | ||
| u.validate(sctx) | ||
| return errs.List | ||
| } | ||
|
|
||
| func (u *UnionSchema) process(ctx *p.SchemaCtx) { | ||
| // Wrap the context and only go to the next one on fail. Keeping all the errors and appending at the end | ||
| listStart := len(ctx.Errors.List) | ||
| for _, s := range u.schemas { | ||
| numIssues := len(ctx.Errors.List) | ||
| s.process(ctx) | ||
| if len(ctx.Errors.List) == numIssues { | ||
| ctx.Errors.List = ctx.Errors.List[:listStart] | ||
| return // success | ||
| } | ||
| } | ||
|
greptile-apps[bot] marked this conversation as resolved.
|
||
| } | ||
|
|
||
| func (u *UnionSchema) validate(ctx *p.SchemaCtx) { | ||
| // Wrap the context and only go to the next one on fail. Keeping all the errors and appending at the end | ||
| listStart := len(ctx.Errors.List) | ||
| for _, s := range u.schemas { | ||
| numIssues := len(ctx.Errors.List) | ||
| s.validate(ctx) | ||
| if len(ctx.Errors.List) == numIssues { | ||
| ctx.Errors.List = ctx.Errors.List[:listStart] | ||
| return // success | ||
| } | ||
| } | ||
|
|
||
|
Oudwins marked this conversation as resolved.
|
||
| } | ||
|
Oudwins marked this conversation as resolved.
|
||
| func (u *UnionSchema) getType() zconst.ZogType { | ||
| return zconst.TypeUnion | ||
| } | ||
| func (u *UnionSchema) setCoercer(c CoercerFunc) {} | ||
|
|
||
| func (u *UnionSchema) toZSS(ctx *ZSSSerializeCtx) *zss.ZSSSchema { | ||
| children := make([]*zss.ZSSSchema, 0, len(u.schemas)) | ||
| for _, schema := range u.schemas { | ||
| children = append(children, schema.toZSS(ctx)) | ||
| } | ||
|
|
||
| return &zss.ZSSSchema{ | ||
| Kind: zconst.TypeUnion, | ||
| Children: children, | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.