-
Notifications
You must be signed in to change notification settings - Fork 16
OneOf (partial implementation) #169
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
Alma-media
wants to merge
19
commits into
master
Choose a base branch
from
one-of-II
base: master
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 all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
7037242
proto
4339fbb
proto
907b790
proto
d82fb38
proto
1bf6762
proto
aa6f3f5
fixes
b5d275c
json
6ecfd23
fixes
2f45961
reuse Message.Compare for OneOf
63f635d
tests for JSON enum
8ec0692
XML oneof support
7ce3adc
Merge branch 'master' into one-of-II
0e11384
remove commented types
4730a61
Merge branch 'one-of-II' of github.com:jexia/semaphore into one-of-II
755ef46
fix openapi tests
db80e8d
fix: validate schema definitions without modifying the properties
jeroenrinzema ebf1436
refactor: todo
jeroenrinzema 1c53b27
fix tests
6702c70
remove useless code
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
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,13 @@ | ||
| log_level = "$LOG_LEVEL" | ||
|
|
||
| protobuffers = ["./proto/*.proto"] | ||
|
|
||
| include = ["flow.hcl"] | ||
|
|
||
| grpc { | ||
| address = ":50051" | ||
| } | ||
|
|
||
| http { | ||
| address = ":8080" | ||
| } | ||
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,23 @@ | ||
| endpoint "typetest" "http" { | ||
| endpoint = "/" | ||
| method = "POST" | ||
| codec = "json" | ||
| } | ||
|
|
||
| endpoint "typetest" "grpc" { | ||
| package = "semaphore.typetest" | ||
| service = "Typetest" | ||
| method = "Run" | ||
| } | ||
|
|
||
| flow "typetest" { | ||
| input "semaphore.typetest.Request" {} | ||
|
|
||
| output "semaphore.typetest.Response" { | ||
| oneof = "{{ input:oneof }}" | ||
|
|
||
| object = "{{ input:object }}" | ||
|
|
||
| array = "{{ input:array }}" | ||
| } | ||
| } |
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,61 @@ | ||
| syntax = "proto3"; | ||
|
|
||
| package semaphore.typetest; | ||
|
|
||
| service Typetest { | ||
| rpc Run(Request) returns (Response) {} | ||
| } | ||
|
|
||
| enum Enum { | ||
| UNKNOWN = 0; | ||
|
|
||
| ON = 1; | ||
|
|
||
| OFF = 2; | ||
| } | ||
|
|
||
| message Data { | ||
| Enum enum = 1; | ||
|
|
||
| string string = 2; | ||
|
|
||
| int64 int64 = 3; | ||
|
|
||
| double double = 4; | ||
|
|
||
| repeated int64 numbers = 5; | ||
| } | ||
|
|
||
| message Array { | ||
| repeated Data data = 1; | ||
| } | ||
|
|
||
| message Request { | ||
| oneof oneof { | ||
| bool empty = 1; | ||
|
|
||
| Data single = 2; | ||
|
|
||
| Array plural = 3; | ||
| } | ||
|
|
||
| Data object = 4; | ||
|
|
||
| repeated Data array = 5; | ||
| } | ||
|
|
||
| message Response { | ||
| oneof oneof { | ||
| bool empty = 1; | ||
|
|
||
| Data single = 2; | ||
|
|
||
| Array plural = 3; | ||
| } | ||
|
|
||
| Data object = 4; | ||
|
|
||
| repeated Data array = 5; | ||
| } | ||
|
|
||
|
|
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,73 @@ | ||
| package json | ||
|
|
||
| import ( | ||
| "errors" | ||
|
|
||
| "github.com/francoispqt/gojay" | ||
| "github.com/jexia/semaphore/pkg/references" | ||
| "github.com/jexia/semaphore/pkg/specs" | ||
| "github.com/jexia/semaphore/pkg/specs/template" | ||
| ) | ||
|
|
||
| // OneOf represents a JSON object | ||
| type OneOf struct { | ||
| path string | ||
| template specs.Template | ||
| store references.Store | ||
| tracker references.Tracker | ||
| isSet bool | ||
| } | ||
|
|
||
| // NewOneOf constructs a new object encoder/decoder for the given specs | ||
| func NewOneOf(path string, template specs.Template, store references.Store, tracker references.Tracker) *OneOf { | ||
| return &OneOf{ | ||
| path: path, | ||
| template: template, | ||
| store: store, | ||
| tracker: tracker, | ||
| } | ||
| } | ||
|
|
||
| // MarshalJSONObject encodes the given specs object into the given gojay encoder | ||
| func (oneOf *OneOf) MarshalJSONObject(encoder *gojay.Encoder) { | ||
| for _, prop := range oneOf.template.OneOf { | ||
| encodeKey(encoder, template.JoinPath(oneOf.path, prop.Name), prop.Name, prop.Template, oneOf.store, oneOf.tracker) | ||
| } | ||
| } | ||
|
|
||
| // UnmarshalJSONObject unmarshals the given specs into the configured reference store | ||
| func (oneOf *OneOf) UnmarshalJSONObject(decoder *gojay.Decoder, key string) error { | ||
| if oneOf.template.OneOf == nil { | ||
| return nil | ||
| } | ||
|
|
||
| if oneOf.isSet { | ||
| return errors.New("only a single field is allowed to be set") | ||
| } | ||
|
|
||
| property, has := oneOf.template.OneOf[key] | ||
| if !has { | ||
| return nil | ||
| } | ||
|
|
||
| // change the state only if the property is known | ||
| oneOf.isSet = true | ||
|
|
||
| oneOf.store.Define(oneOf.path, len(oneOf.template.Message)) | ||
|
|
||
| return decode(decoder, template.JoinPath(oneOf.path, key), property.Template, oneOf.store, oneOf.tracker) | ||
| } | ||
|
|
||
| // NKeys returns the amount of available keys inside the given oneof | ||
| func (oneOf *OneOf) NKeys() int { | ||
| if oneOf.template.OneOf == nil { | ||
| return 0 | ||
| } | ||
|
|
||
| return len(oneOf.template.OneOf) | ||
| } | ||
|
|
||
| // IsNil returns whether the given oneof is null or not | ||
| func (oneOf *OneOf) IsNil() bool { | ||
| return oneOf == nil | ||
| } |
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
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.
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.
I think it is best to exclude this example from the
masterbranch.