-
Notifications
You must be signed in to change notification settings - Fork 1
Implementing few features #1
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
kanishka-linux
wants to merge
98
commits into
activesphere:master
Choose a base branch
from
kanishka-linux:master
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
98 commits
Select commit
Hold shift + click to select a range
ba18332
add more tests
kanishka-linux c3c6789
modify Jake module
kanishka-linux 0907115
handle type key when merging
kanishka-linux 8334625
modify Array module
kanishka-linux d7cd346
add separate module for cases when type not present
kanishka-linux 63a16bf
handle multipleOf property
kanishka-linux 7f57601
modify Object module
kanishka-linux 61ab36b
modify String module
kanishka-linux 693fc77
remove files
kanishka-linux 42a1bed
add and modify dependencies
kanishka-linux 57923ba
add comment
kanishka-linux 47485ad
lower order of gen_all for enum
kanishka-linux 5313a2b
modify getmultipleof and get_float_number
kanishka-linux c464a02
remove usage of bind_filter from stringer
kanishka-linux e2cf5f7
use deep_merge again in in allOf gen_init
kanishka-linux d302482
handle map item and list items separately
kanishka-linux 0d61b1e
add separate get_min_max method to fix exclusiveMaximum feature
kanishka-linux 574d7a9
fix test_generator method
kanishka-linux 4d8c737
add rudimentary support for patternProperties
kanishka-linux 9bdc74a
add patternProperties to prop map
kanishka-linux 87fae25
add dependencies support for object
kanishka-linux 6887e97
remove unnecessary line
kanishka-linux f4abe43
add tests for object dependencies
kanishka-linux 9a4dc7f
add dependencies key to prop map
kanishka-linux ee27150
handle dependencies when property is null
kanishka-linux e598f80
handle test cases of dependencies.json
kanishka-linux da4e3fa
add default.json
kanishka-linux b3803e9
separate tuple and list generation methods
kanishka-linux db8a83d
remove get_float_number method
kanishka-linux 2a84eb7
add decide_min_max with error message
kanishka-linux 265ae51
add test cases for oneOf feature
kanishka-linux 9c97664
add support for oneOf feature
kanishka-linux 33f8a68
add not tests
kanishka-linux 2f1f279
allow returning only type name if type is not nil
kanishka-linux c844aba
check for not feature when refining properties
kanishka-linux 36f9176
add support for not feature
kanishka-linux b6aaa84
fix generator for patternProperties
kanishka-linux fa4e0da
experiment with property based testing
kanishka-linux 6c0bbf1
add map parameter to gen_enum
kanishka-linux ae8e0c0
validate enum values against existing schema
kanishka-linux 1385cb3
add test enum with constraints
kanishka-linux 4d7ccbf
modify gen_init and add gen_init_root
kanishka-linux ba76b77
add separate Mixed module
kanishka-linux e90b467
modify gen_enum
kanishka-linux 923a3b5
remove unnecessary code
kanishka-linux 99c4dba
remove type checking from gen_enum
kanishka-linux 48ff9d0
remove type_map and code clean-up
kanishka-linux ed96a02
remove gen_enum
kanishka-linux ffca40c
replace gen_number_init with gen_number
kanishka-linux b7a8427
remove type_list and rearrange code
kanishka-linux 592bdb1
replace stringer with gen_string and remove enum as function parameter
kanishka-linux 445acb9
add support for ref
kanishka-linux b371e98
add omap parameter to gen_array
kanishka-linux 2608cb3
rearrange code and add omap parameter to gen_mixed
kanishka-linux ab21124
add omap parameter to gen_notype
kanishka-linux 953499b
add omap parameter to gen_number
kanishka-linux 97be757
modify functions to support ref feature
kanishka-linux 968bc6e
add Ref module
kanishka-linux 3264a8d
add tests for ref feature
kanishka-linux 5d2f500
convert url string to charlist before using it with httpc
kanishka-linux 6c434d2
add complex ref test
kanishka-linux fdeaa40
add support for recursive ref
kanishka-linux 7a3316b
add recursive ref tests
kanishka-linux 0270d3c
add one more $ref test
kanishka-linux edef4da
generate empty list when item map is empty
kanishka-linux 1fcb373
resize generator for recursive ref
kanishka-linux 1393cc9
add check_ref parameter to expand_ref
kanishka-linux 17540f4
add size parameter to generator function
kanishka-linux 1c8b801
remove debug info
kanishka-linux a145b7e
make size double initially
kanishka-linux 8a9a80d
use bind in gen_init
kanishka-linux a4fd93d
remove debug info
kanishka-linux 7dfcff3
format code
kanishka-linux def8fde
add separate get_lazy_streamkey function
kanishka-linux 84e40bc
obtain tuple when expanding ref
kanishka-linux 762da75
return whether ref exists or not along with map
kanishka-linux ec064bf
check ref key if type not present
kanishka-linux 6d3cb88
check whether ref points to root or not
kanishka-linux 622947e
modify get_lazy_streamkey
kanishka-linux 3dfb63d
fix not property
kanishka-linux 75c974c
remove checking ref
kanishka-linux d31413d
add separate expand_ref for root ref
kanishka-linux 6fa2978
rewrite filter for debugging
kanishka-linux 0068f16
modify complex recursive ref test
kanishka-linux 0ad2def
add few more tests for checking "not" property
kanishka-linux 2dcc315
remove check_ref_string and related functions
kanishka-linux 3383b23
remove check_ref parameter from expand_ref
kanishka-linux dc103d3
fix typo
kanishka-linux 4acc525
remove unused variables warning
kanishka-linux 4a63d2a
mix format
kanishka-linux 3b5f6f1
remove omap and size parameters from gen_object
kanishka-linux 1ca1c20
remove omap and size parameters from gen_number
kanishka-linux 7a86668
remove size parameter from gen_string
kanishka-linux b025f98
remove omap and size parameters from gen_array
kanishka-linux 79adbb5
remove omap and size parameters from gen_mixed
kanishka-linux 7baa611
remove omap and size parameters from gen_notype
kanishka-linux a2fb44a
mix format
kanishka-linux f8dc911
pass single schema parameter to gen_init
kanishka-linux 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 |
|---|---|---|
| @@ -1,5 +1,108 @@ | ||
| defmodule Jake.Array do | ||
| def gen(_) do | ||
| StreamData.constant([]) | ||
| @type_list [ | ||
| %{"type" => "integer"}, | ||
| %{"type" => "number"}, | ||
| %{"type" => "boolean"}, | ||
| %{"type" => "string"}, | ||
| %{"type" => "null"}, | ||
| nil | ||
| ] | ||
|
|
||
| @min_items 0 | ||
|
|
||
| @max_items 1000 | ||
|
|
||
| def gen_array(%{"items" => items} = map, schema) do | ||
| case items do | ||
| item when is_map(item) and map_size(item) == 0 -> | ||
| StreamData.constant([]) | ||
|
|
||
| item when is_map(item) -> | ||
| gen_list(map, item, schema) | ||
|
|
||
| item when is_list(item) -> | ||
| gen_tuple(map, item, schema) | ||
|
|
||
| _ -> | ||
| raise "Invalid items in array" | ||
| end | ||
| end | ||
|
|
||
| def gen_array(map, schema), do: arraytype(map, map["items"], schema) | ||
|
|
||
| def arraytype(map, items, schema) when is_nil(items) do | ||
| item = get_one_of(schema) | ||
| {min, max} = get_min_max(map) | ||
| decide_min_max(map, item, min, max) | ||
| end | ||
|
|
||
| def gen_tuple(map, items, schema) do | ||
| list = for n <- items, is_map(n), do: Map.put(schema, "map", n) |> Jake.gen_init() | ||
|
|
||
| {min, max} = get_min_max(map) | ||
|
|
||
| case map["additionalItems"] do | ||
| x when is_map(x) -> | ||
| add_additional_items(list, Jake.gen_init(Map.put(schema, "map", x)), max, min) | ||
|
|
||
| x when (is_boolean(x) and x) or is_nil(x) -> | ||
| add_additional_items(list, get_one_of(schema), max, min) | ||
|
|
||
| x when is_boolean(x) and not x and length(list) in min..max -> | ||
| StreamData.fixed_list(list) | ||
|
|
||
| _ -> | ||
| raise "Invalid items or length of list exceeds specified bounds" | ||
| end | ||
| end | ||
|
|
||
| def gen_list(map, items, schema) do | ||
| {min, max} = get_min_max(map) | ||
| item = Map.put(schema, "map", items) |> Jake.gen_init() | ||
| decide_min_max(map, item, min, max) | ||
| end | ||
|
|
||
| def get_min_max(map) do | ||
| min = Map.get(map, "minItems", @min_items) | ||
| max = Map.get(map, "maxItems", @max_items) | ||
| {min, max} | ||
| end | ||
|
|
||
| def decide_min_max(map, item, min, max) | ||
| when is_integer(min) and is_integer(max) and min < max do | ||
|
Member
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. What happens if this doesn't match? If it should throw error, then being explicit about it might be better. |
||
| if map["uniqueItems"] do | ||
| StreamData.uniq_list_of(item, min_length: min, max_length: max) | ||
| else | ||
| StreamData.list_of(item, min_length: min, max_length: max) | ||
| end | ||
| end | ||
|
|
||
| def decide_min_max(map, item, min, max) do | ||
| raise "Bounds of items not well defined" | ||
| end | ||
|
|
||
| def get_one_of(schema) do | ||
| for(n <- @type_list, is_map(n), do: Map.put(schema, "map", n) |> Jake.gen_init()) | ||
| |> StreamData.one_of() | ||
| end | ||
|
|
||
| def add_additional_items(olist, additional, max, min) do | ||
| StreamData.bind(StreamData.fixed_list(olist), fn list -> | ||
| StreamData.bind_filter( | ||
| StreamData.list_of(additional), | ||
| fn | ||
| nlist | ||
| when (length(list) + length(nlist)) in min..max -> | ||
| {:cont, StreamData.constant(list ++ nlist)} | ||
|
|
||
| nlist | ||
| when length(list) in min..max -> | ||
| {:cont, StreamData.constant(list)} | ||
|
|
||
| _ -> | ||
| :skip | ||
| end | ||
| ) | ||
| end) | ||
| end | ||
| end | ||
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,33 @@ | ||
| # Ref: https://stackoverflow.com/questions/38864001/elixir-how-to-deep-merge-maps | ||
| # Ref: https://github.com/activesphere/jake/blob/master/lib/jake/map_util.ex | ||
|
|
||
| defmodule Jake.MapUtil do | ||
| def deep_merge(left, right) do | ||
| Map.merge(left, right, &deep_resolve/3) | ||
| end | ||
|
|
||
| defp deep_resolve(_key, left, nil) do | ||
| left | ||
| end | ||
|
|
||
| defp deep_resolve(_key, nil, right) do | ||
| right | ||
| end | ||
|
|
||
| defp deep_resolve(key, left, right) when key == "type" do | ||
| case {is_list(left), is_list(right)} do | ||
| {x, y} when x and y -> left ++ right | ||
| {x, y} when x and not y -> left ++ [right] | ||
| {x, y} when not x and y -> [left] ++ right | ||
| {x, y} when not x and not y -> [left, right] | ||
| end | ||
| end | ||
|
|
||
| defp deep_resolve(_key, left, right) when is_map(left) do | ||
| Map.merge(left, right) | ||
| end | ||
|
|
||
| defp deep_resolve(_key, left, right) when is_list(left) do | ||
| left ++ right | ||
| end | ||
| end |
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 @@ | ||
| defmodule Jake.Mixed do | ||
| @types [ | ||
| "array", | ||
| "boolean", | ||
| "integer", | ||
| "null", | ||
| "number", | ||
| "object", | ||
| "string" | ||
| ] | ||
|
|
||
| def gen_mixed(%{"anyOf" => options} = map, schema) when is_list(options) do | ||
| nmap = Map.drop(map, ["anyOf"]) | ||
|
|
||
| nlist = for(n <- options, is_map(n), do: Map.merge(nmap, n)) | ||
| for(n <- nlist, do: Map.put(schema, "map", n) |> Jake.gen_init()) |> StreamData.one_of() | ||
| end | ||
|
|
||
| def gen_mixed(%{"oneOf" => options} = map, schema) when is_list(options) do | ||
| nmap = Map.drop(map, ["oneOf"]) | ||
|
|
||
| tail_schema = fn tail -> | ||
| Enum.reduce(tail, %{}, fn x, acc -> Jake.MapUtil.deep_merge(acc, x) end) | ||
| end | ||
|
|
||
| nlist = | ||
| for {n, counter} <- Enum.with_index(options) do | ||
| hd = Map.put(schema, "map", Map.merge(nmap, n)) |> Jake.gen_init() | ||
| tail = List.delete_at(options, counter) |> tail_schema.() | ||
| {hd, tail} | ||
| end | ||
|
|
||
| try_one_of(nlist, 0) | ||
| end | ||
|
|
||
| def gen_mixed(%{"allOf" => options} = map, schema) when is_list(options) do | ||
| nmap = Map.drop(map, ["allOf"]) | ||
|
|
||
| map = | ||
| Enum.reduce(options, %{}, fn x, acc -> Jake.MapUtil.deep_merge(acc, x) end) | ||
| |> Jake.MapUtil.deep_merge(nmap) | ||
|
|
||
| Map.put(schema, "map", map) |> Jake.gen_init() | ||
| end | ||
|
|
||
| def gen_mixed(%{"not" => not_schema} = map, schema) when is_map(not_schema) do | ||
| nmap = Map.drop(map, ["not"]) | ||
| nmap_type = nmap["type"] | ||
|
|
||
| type_val = | ||
| if not_schema["type"] do | ||
| not_schema["type"] | ||
| else | ||
| Jake.Notype.gen_notype("return type", schema) | ||
| end | ||
|
|
||
| type = if type_val == nil, do: "null", else: type_val | ||
| nlist = if is_list(type), do: @types -- type, else: @types -- [type] | ||
|
|
||
| data = | ||
| if nmap_type || (is_map(nmap) && map_size(nmap) > 0) do | ||
| Map.put(schema, "map", nmap) |> Jake.gen_init() | ||
| else | ||
| for(n <- nlist, do: Map.put(schema, "map", %{"type" => n}) |> Jake.gen_init()) | ||
| |> StreamData.one_of() | ||
| end | ||
|
|
||
| StreamData.filter( | ||
| data, | ||
| fn x -> | ||
| if type == "null" do | ||
| true | ||
| else | ||
| not ExJsonSchema.Validator.valid?(not_schema, x) | ||
| end | ||
| end | ||
| ) | ||
| end | ||
|
|
||
| def try_one_of(nlist, index) do | ||
| data = filter_mutually_exclusive(nlist, index) | ||
|
|
||
| try do | ||
| Enum.take(data, 25) | ||
| data | ||
| rescue | ||
| _ -> filter_mutually_exclusive(nlist, index + 1) | ||
| end | ||
| end | ||
|
|
||
| def filter_mutually_exclusive(nlist, index) do | ||
| if index < length(nlist) do | ||
| {head, tail_schema} = Enum.at(nlist, index) | ||
| StreamData.filter(head, fn hd -> not ExJsonSchema.Validator.valid?(tail_schema, hd) end) | ||
| else | ||
| raise "oneOf combination not possible" | ||
| end | ||
| end | ||
| end |
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.
How about
Avoid extra indirection? Otherwise it becomes difficult to understand the code.
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.
This is much better. I'll apply necessary changes.