structured 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +41 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/structured.rb +6 -0
- data/lib/structured/boolean.rb +15 -0
- data/lib/structured/dictionary.rb +51 -0
- data/lib/structured/errors.rb +10 -0
- data/lib/structured/errors/base.rb +12 -0
- data/lib/structured/errors/multiple_errors.rb +38 -0
- data/lib/structured/integer.rb +15 -0
- data/lib/structured/list.rb +45 -0
- data/lib/structured/nullable.rb +24 -0
- data/lib/structured/parser_stack.rb +40 -0
- data/lib/structured/string.rb +15 -0
- data/lib/structured/struct.rb +176 -0
- data/lib/structured/type.rb +15 -0
- data/lib/structured/version.rb +3 -0
- data/structured.gemspec +28 -0
- metadata +111 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 0bcf89081dd0d1c3f4c8923c81c35168114af665a723c72c9152db225b131830
|
4
|
+
data.tar.gz: f04d25bb5905adc4b16da6c0b419eed45cacb904c026fb4793c80b3ed3e21f95
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6ae01f1d392e7f9bb33a3736d38b4683b0579509aaeff132857d0ce53b8899f43a20cdcdaec90ce560c041959ae058dbb48bbd0e10ff45eb0dcacc035624efba
|
7
|
+
data.tar.gz: 6644203a5c701db6f3be3115235adfd8d53f961e81a11f9f6f4a52c62914cbe1c3aa472e3548ca8a99696f5f41c9144a5ed923448d8f59f192ed86069e9fe1c9
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at julian@jnadeau.ca. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Shopify
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# Structured
|
2
|
+
|
3
|
+
Parse and validate hashes into structured Ruby objects.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'Structured'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install Structured
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
TODO: Write usage instructions here
|
24
|
+
|
25
|
+
## Development
|
26
|
+
|
27
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
28
|
+
|
29
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
30
|
+
|
31
|
+
## Contributing
|
32
|
+
|
33
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/Shopify/structured. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
34
|
+
|
35
|
+
## License
|
36
|
+
|
37
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
38
|
+
|
39
|
+
## Code of Conduct
|
40
|
+
|
41
|
+
Everyone interacting in the Structured project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/Shopify/structured/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "structured"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/lib/structured.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
module Structured
|
2
|
+
class Boolean
|
3
|
+
extend Structured::Type
|
4
|
+
|
5
|
+
def self.parse(yaml_value, stack:, context: nil)
|
6
|
+
unless yaml_value == true || yaml_value == false
|
7
|
+
raise Structured::Errors::InvalidValue.new(
|
8
|
+
stack,
|
9
|
+
"#{stack} expects a boolean, but found: #{yaml_value.inspect}"
|
10
|
+
)
|
11
|
+
end
|
12
|
+
yaml_value
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module Structured
|
2
|
+
class Dictionary
|
3
|
+
include Structured::Type
|
4
|
+
|
5
|
+
attr_reader :member_klass
|
6
|
+
|
7
|
+
def initialize(member_klass)
|
8
|
+
@member_klass = member_klass
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.[](member_klass)
|
12
|
+
new(member_klass)
|
13
|
+
end
|
14
|
+
|
15
|
+
def parse(parsed_yaml, stack:, context: nil)
|
16
|
+
unless parsed_yaml.is_a?(Hash)
|
17
|
+
raise Structured::Errors::InvalidValue.new(stack,
|
18
|
+
"#{stack} expects a hash, but found #{parsed_yaml.class.name}")
|
19
|
+
end
|
20
|
+
|
21
|
+
result = {}
|
22
|
+
errors = []
|
23
|
+
parsed_yaml.each do |key, value|
|
24
|
+
begin
|
25
|
+
unless key.is_a?(::String)
|
26
|
+
raise Structured::Errors::InvalidValue.new(stack,
|
27
|
+
"Dictionary key #{key.inspect} is not a string")
|
28
|
+
end
|
29
|
+
result[key] = member_klass.parse(value, stack: stack.push(key, member_klass), context: context)
|
30
|
+
rescue Structured::Errors::Base => exception
|
31
|
+
errors << exception
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
raise Structured::Errors::MultipleErrors.union(stack, errors) unless errors.empty?
|
36
|
+
result
|
37
|
+
end
|
38
|
+
|
39
|
+
def format_stack_frame_element(key)
|
40
|
+
"[#{key}]"
|
41
|
+
end
|
42
|
+
|
43
|
+
def default_value
|
44
|
+
{}.freeze
|
45
|
+
end
|
46
|
+
|
47
|
+
def type_name
|
48
|
+
"Dictionary[#{member_klass.type_name}]"
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
require "structured/errors/base"
|
2
|
+
require "structured/errors/multiple_errors"
|
3
|
+
|
4
|
+
module Structured
|
5
|
+
module Errors
|
6
|
+
class InvalidValue < Structured::Errors::Base; end
|
7
|
+
class MissingKey < Structured::Errors::Base; end
|
8
|
+
class UnknownKey < Structured::Errors::Base; end
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module Structured
|
2
|
+
module Errors
|
3
|
+
class MultipleErrors < Base
|
4
|
+
attr_reader :root, :errors
|
5
|
+
|
6
|
+
def initialize(stack, errors)
|
7
|
+
@errors = errors
|
8
|
+
|
9
|
+
messages = errors.map { |msg| "- #{msg}" }.join("\n")
|
10
|
+
super(stack, "Failed to parse #{stack.type.type_name} due to #{errors.length} errors:\n#{messages}")
|
11
|
+
end
|
12
|
+
|
13
|
+
def annotation(header: true)
|
14
|
+
annotations = errors.map { |error| error.annotation(header: false) }
|
15
|
+
if header
|
16
|
+
head = "Failed to compile #{CGI.escape_html(stack.root.type.type_name)} due to #{errors.length} errors:"
|
17
|
+
head + "\n\n" + annotations.join("\n\n")
|
18
|
+
else
|
19
|
+
annotations.join("\n\n")
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.union(stack, exceptions)
|
24
|
+
return exceptions.first if exceptions.size == 1
|
25
|
+
|
26
|
+
flattened_exceptions = exceptions.flat_map do |exception|
|
27
|
+
case exception
|
28
|
+
when MultipleErrors then exception.errors
|
29
|
+
when Base then [exception]
|
30
|
+
else raise "Cannot wrap a #{exception.class.name} in a ParsingFailed exception"
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
new(stack, flattened_exceptions)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module Structured
|
2
|
+
class Integer
|
3
|
+
extend Structured::Type
|
4
|
+
|
5
|
+
def self.parse(yaml_value, stack:, context: nil)
|
6
|
+
unless yaml_value.is_a?(::Integer)
|
7
|
+
raise Structured::Errors::InvalidValue.new(
|
8
|
+
stack,
|
9
|
+
"#{stack} expects an integer, but found: #{yaml_value.inspect}"
|
10
|
+
)
|
11
|
+
end
|
12
|
+
yaml_value
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
module Structured
|
2
|
+
class List
|
3
|
+
include Structured::Type
|
4
|
+
|
5
|
+
attr_reader :member_klass
|
6
|
+
|
7
|
+
def initialize(member_klass)
|
8
|
+
@member_klass = member_klass
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.[](member_klass)
|
12
|
+
new(member_klass)
|
13
|
+
end
|
14
|
+
|
15
|
+
def parse(parsed_yaml, stack:, context: nil)
|
16
|
+
unless parsed_yaml.is_a?(Array)
|
17
|
+
raise Structured::Errors::InvalidValue.new(stack, "#{stack} expects an array")
|
18
|
+
end
|
19
|
+
|
20
|
+
errors = []
|
21
|
+
result = parsed_yaml.map.with_index do |value, index|
|
22
|
+
begin
|
23
|
+
member_klass.parse(value, stack: stack.push(index, member_klass), context: context)
|
24
|
+
rescue Structured::Errors::Base => exception
|
25
|
+
errors << exception
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
raise Structured::Errors::MultipleErrors.union(stack, errors) unless errors.empty?
|
30
|
+
result
|
31
|
+
end
|
32
|
+
|
33
|
+
def format_stack_frame_element(index)
|
34
|
+
"[#{index}]"
|
35
|
+
end
|
36
|
+
|
37
|
+
def default_value
|
38
|
+
[].freeze
|
39
|
+
end
|
40
|
+
|
41
|
+
def type_name
|
42
|
+
"List[#{member_klass.type_name}]"
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module Structured
|
2
|
+
class Nullable
|
3
|
+
def self.[](type)
|
4
|
+
new(type)
|
5
|
+
end
|
6
|
+
|
7
|
+
def initialize(type)
|
8
|
+
@type = type
|
9
|
+
end
|
10
|
+
|
11
|
+
def default_value
|
12
|
+
nil
|
13
|
+
end
|
14
|
+
|
15
|
+
def parse(yaml_value, stack:, context: nil)
|
16
|
+
return yaml_value if yaml_value.nil?
|
17
|
+
@type.parse(yaml_value, stack: stack, context: context)
|
18
|
+
end
|
19
|
+
|
20
|
+
def type_name
|
21
|
+
"Nullable[#{@type.type_name}]"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module Structured
|
2
|
+
class ParserStack
|
3
|
+
attr_accessor :type, :element, :parent
|
4
|
+
|
5
|
+
def initialize(type:, element:, parent:)
|
6
|
+
@type = type
|
7
|
+
@element = element
|
8
|
+
@parent = parent
|
9
|
+
end
|
10
|
+
|
11
|
+
def push(element, type)
|
12
|
+
self.class.new(type: type, element: element, parent: self)
|
13
|
+
end
|
14
|
+
|
15
|
+
def path
|
16
|
+
if root?
|
17
|
+
type.type_name
|
18
|
+
elsif element.nil?
|
19
|
+
# abtract elements like Step use no element, they just forward parsing to a concrete type.
|
20
|
+
parent.path
|
21
|
+
else
|
22
|
+
parent.path + parent.type.format_stack_frame_element(element)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
alias_method :to_s, :path
|
27
|
+
|
28
|
+
def root?
|
29
|
+
parent.nil?
|
30
|
+
end
|
31
|
+
|
32
|
+
def root
|
33
|
+
root? ? self : parent.root
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.root(type)
|
37
|
+
new(type: type, element: nil, parent: nil)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module Structured
|
2
|
+
class String
|
3
|
+
extend Structured::Type
|
4
|
+
|
5
|
+
def self.parse(yaml_value, stack:, context: nil)
|
6
|
+
unless yaml_value.is_a?(::String)
|
7
|
+
raise Structured::Errors::InvalidValue.new(
|
8
|
+
stack,
|
9
|
+
"#{stack} expects a string, but found: #{yaml_value.inspect}"
|
10
|
+
)
|
11
|
+
end
|
12
|
+
yaml_value
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,176 @@
|
|
1
|
+
require 'structured/type'
|
2
|
+
require 'structured/parser_stack'
|
3
|
+
require 'structured/nullable'
|
4
|
+
|
5
|
+
require 'structured/boolean'
|
6
|
+
require 'structured/dictionary'
|
7
|
+
require 'structured/integer'
|
8
|
+
require 'structured/list'
|
9
|
+
require 'structured/string'
|
10
|
+
|
11
|
+
module Structured
|
12
|
+
module Struct
|
13
|
+
# Alias these types here so these constant are part of the lookup path
|
14
|
+
# when being used in our struct definitions.
|
15
|
+
Boolean = Structured::Boolean
|
16
|
+
String = Structured::String
|
17
|
+
Integer = Structured::Integer
|
18
|
+
Dictionary = Structured::Dictionary
|
19
|
+
List = Structured::List
|
20
|
+
Nullable = Structured::Nullable
|
21
|
+
|
22
|
+
def self.included(base)
|
23
|
+
base.extend(ClassMethods)
|
24
|
+
end
|
25
|
+
|
26
|
+
def initialize
|
27
|
+
@attributes = {}
|
28
|
+
end
|
29
|
+
|
30
|
+
def generate(_context)
|
31
|
+
attrs = self.class.attributes.select do |name, _|
|
32
|
+
has_attribute?(name)
|
33
|
+
end
|
34
|
+
|
35
|
+
attrs.keys.map { |name| [name, public_send(name)] }.to_h
|
36
|
+
end
|
37
|
+
|
38
|
+
def read_attribute(name)
|
39
|
+
@attributes.fetch(name) { self.class.attributes[name].default_value }
|
40
|
+
end
|
41
|
+
|
42
|
+
def has_attribute?(name)
|
43
|
+
!@attributes[name].nil?
|
44
|
+
end
|
45
|
+
|
46
|
+
def write_attribute(name, value)
|
47
|
+
@attributes[name] = value
|
48
|
+
end
|
49
|
+
|
50
|
+
module ClassMethods
|
51
|
+
include Structured::Type
|
52
|
+
|
53
|
+
def attributes
|
54
|
+
@attributes ||= {}
|
55
|
+
end
|
56
|
+
|
57
|
+
def required_attribute(name, type)
|
58
|
+
define_accessor(name.to_s)
|
59
|
+
attributes[name.to_s] = Attribute.new(self, type)
|
60
|
+
end
|
61
|
+
|
62
|
+
def optional_attribute(name, type, default: Attribute::UNDEFINED)
|
63
|
+
define_accessor(name.to_s)
|
64
|
+
default = default == Attribute::UNDEFINED ? type.default_value : default
|
65
|
+
attributes[name.to_s] = Attribute.new(self, type, default_value: default)
|
66
|
+
end
|
67
|
+
|
68
|
+
def ignore_attribute(name)
|
69
|
+
attributes[name.to_s] = :ignore
|
70
|
+
end
|
71
|
+
|
72
|
+
def parse(yaml_hash, stack:, context: nil)
|
73
|
+
parsed_instance = new
|
74
|
+
parse_attributes(yaml_hash || {}, stack: stack, context: context).each do |attribute, value|
|
75
|
+
parsed_instance.send(:"#{attribute}=", value)
|
76
|
+
end
|
77
|
+
|
78
|
+
parsed_instance
|
79
|
+
end
|
80
|
+
|
81
|
+
def new_with_defaults
|
82
|
+
new_instance = new
|
83
|
+
attributes.each do |name, attribute|
|
84
|
+
next if attribute == :ignore
|
85
|
+
new_instance.send(:"#{name}=", attribute.default_value) if attribute.has_default?
|
86
|
+
end
|
87
|
+
new_instance
|
88
|
+
end
|
89
|
+
|
90
|
+
def format_stack_frame_element(name)
|
91
|
+
".#{name}"
|
92
|
+
end
|
93
|
+
|
94
|
+
protected
|
95
|
+
|
96
|
+
def define_accessor(name)
|
97
|
+
attribute_accessors_module.send(:define_method, name) do
|
98
|
+
read_attribute(name)
|
99
|
+
end
|
100
|
+
|
101
|
+
attribute_accessors_module.send(:define_method, "#{name}?") do
|
102
|
+
has_attribute?(name)
|
103
|
+
end
|
104
|
+
|
105
|
+
attribute_accessors_module.send(:define_method, "#{name}=") do |value|
|
106
|
+
write_attribute(name, value)
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
def attribute_accessors_module
|
111
|
+
self::AttributeAccessors ||= begin
|
112
|
+
mod = Module.new
|
113
|
+
include(mod)
|
114
|
+
mod
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
def parse_attributes(yaml_hash, allow_unknowns: false, stack:, context:)
|
119
|
+
unless yaml_hash.is_a?(Hash)
|
120
|
+
raise Structured::Errors::InvalidValue.new(stack,
|
121
|
+
"#{stack} expects a hash, but found #{yaml_hash.class.name}")
|
122
|
+
end
|
123
|
+
|
124
|
+
result = {}
|
125
|
+
errors = []
|
126
|
+
attributes.each do |name, attribute|
|
127
|
+
begin
|
128
|
+
next if attribute == :ignore
|
129
|
+
|
130
|
+
if yaml_hash.key?(name)
|
131
|
+
result[name.to_sym] = attribute.type.parse(yaml_hash[name],
|
132
|
+
stack: stack.push(name, attribute.type), context: context)
|
133
|
+
elsif !attribute.has_default?
|
134
|
+
raise Structured::Errors::MissingKey.new(stack,
|
135
|
+
"#{stack} (#{type_name}) requires the following key: #{name}")
|
136
|
+
end
|
137
|
+
rescue Structured::Errors::Base => exception
|
138
|
+
errors << exception
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
unknown_keys = yaml_hash.keys - attributes.keys.map(&:to_s)
|
143
|
+
unless allow_unknowns || unknown_keys.empty?
|
144
|
+
unknown_keys.each do |key|
|
145
|
+
errors << Structured::Errors::UnknownKey.new(stack,
|
146
|
+
"#{stack} (#{type_name}) does not support a #{key} field")
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
raise Structured::Errors::MultipleErrors.union(stack, errors) unless errors.empty?
|
151
|
+
result
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
class Attribute
|
156
|
+
UNDEFINED = Object.new
|
157
|
+
|
158
|
+
attr_reader :parent, :type
|
159
|
+
|
160
|
+
def initialize(parent, type, default_value: UNDEFINED)
|
161
|
+
@parent = parent
|
162
|
+
@type = type
|
163
|
+
@default_value = default_value
|
164
|
+
end
|
165
|
+
|
166
|
+
def has_default?
|
167
|
+
@default_value != UNDEFINED
|
168
|
+
end
|
169
|
+
|
170
|
+
def default_value
|
171
|
+
@default_value unless @default_value == UNDEFINED
|
172
|
+
end
|
173
|
+
end
|
174
|
+
private_constant :Attribute
|
175
|
+
end
|
176
|
+
end
|
data/structured.gemspec
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "structured/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "structured"
|
8
|
+
spec.version = Structured::VERSION
|
9
|
+
spec.authors = ["jules2689"]
|
10
|
+
spec.email = ["julian@jnadeau.ca"]
|
11
|
+
|
12
|
+
spec.summary = 'Parse and validate hashes into structured Ruby objects.'
|
13
|
+
spec.description = 'Parse and validate hashes into structured Ruby objects.'
|
14
|
+
spec.homepage = "https://github.com/shopify/structured"
|
15
|
+
spec.license = "MIT"
|
16
|
+
spec.metadata["allowed_push_host"] = "https://rubygems.org"
|
17
|
+
|
18
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
19
|
+
f.match(%r{^(test|spec|features)/})
|
20
|
+
end
|
21
|
+
spec.bindir = "exe"
|
22
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
23
|
+
spec.require_paths = ["lib"]
|
24
|
+
|
25
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
26
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
27
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: structured
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- jules2689
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-01-26 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.16'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.16'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '5.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '5.0'
|
55
|
+
description: Parse and validate hashes into structured Ruby objects.
|
56
|
+
email:
|
57
|
+
- julian@jnadeau.ca
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".travis.yml"
|
64
|
+
- CODE_OF_CONDUCT.md
|
65
|
+
- Gemfile
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- bin/console
|
70
|
+
- bin/setup
|
71
|
+
- lib/structured.rb
|
72
|
+
- lib/structured/boolean.rb
|
73
|
+
- lib/structured/dictionary.rb
|
74
|
+
- lib/structured/errors.rb
|
75
|
+
- lib/structured/errors/base.rb
|
76
|
+
- lib/structured/errors/multiple_errors.rb
|
77
|
+
- lib/structured/integer.rb
|
78
|
+
- lib/structured/list.rb
|
79
|
+
- lib/structured/nullable.rb
|
80
|
+
- lib/structured/parser_stack.rb
|
81
|
+
- lib/structured/string.rb
|
82
|
+
- lib/structured/struct.rb
|
83
|
+
- lib/structured/type.rb
|
84
|
+
- lib/structured/version.rb
|
85
|
+
- structured.gemspec
|
86
|
+
homepage: https://github.com/shopify/structured
|
87
|
+
licenses:
|
88
|
+
- MIT
|
89
|
+
metadata:
|
90
|
+
allowed_push_host: https://rubygems.org
|
91
|
+
post_install_message:
|
92
|
+
rdoc_options: []
|
93
|
+
require_paths:
|
94
|
+
- lib
|
95
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
96
|
+
requirements:
|
97
|
+
- - ">="
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: '0'
|
100
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
requirements: []
|
106
|
+
rubyforge_project:
|
107
|
+
rubygems_version: 2.7.4
|
108
|
+
signing_key:
|
109
|
+
specification_version: 4
|
110
|
+
summary: Parse and validate hashes into structured Ruby objects.
|
111
|
+
test_files: []
|