typespec 0.0.0.0 → 0.1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +25 -4
- data/lib/typespec.rb +179 -0
- data/lib/typespec/gem.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 89735c9c1f915fb52dcbbf55eb6d01875b4edac2
|
4
|
+
data.tar.gz: 63067969f72f7b18b3b129e58c78c128f025f36b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b7414951bc78d23b602e6b5cabf7328c7da83bf72268f28fb824643aec3de3efb17f6425e415668b4284bc2629022f7355960e72fc70f8b224cbed5948f4d50d
|
7
|
+
data.tar.gz: 1b12cb00d9639a1ace0e92b16e71b7b8ed6a49c587b1e580b4f2fd4d7adac9acdb844f742e5d2fd31ec1b75580c2a23e5fa78332f334f6201fed56c969e37fdf
|
data/README.md
CHANGED
@@ -8,8 +8,29 @@
|
|
8
8
|
:nut_and_bolt: Specify complex schema made of types or specific values.
|
9
9
|
|
10
10
|
```Ruby
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
11
|
+
module Defines
|
12
|
+
def self.typespec
|
13
|
+
Typespec.hash[Typespec.string => Typespec.or[Typespec.nothing,
|
14
|
+
Typespec.boolean,
|
15
|
+
Typespec.number,
|
16
|
+
Typespec.string]]
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
Defines.typespec.matches?('YETI_LINKAGE' => 'YETI_LINKAGE_STATIC',
|
21
|
+
'__YETI_IS_BEING_COMPILED__' => true,
|
22
|
+
'YETI_CONFIGURATION' => 'YETI_CONFIGURATION_DEBUG',
|
23
|
+
'_HAS_EXCEPTIONS' => false,
|
24
|
+
'_SCL_SECURE_NO_WARNINGS' => true,
|
25
|
+
'_CRT_SECURE_NO_WARNINGS' => true,
|
26
|
+
'_CRT_SECURE_NO_DEPRECATE' => true,
|
27
|
+
'_SECURE_SCL_THROWS' => false,
|
28
|
+
'_SILENCE_DEPRECATION_OF_SECURE_SCL_THROWS' => true,
|
29
|
+
'_USING_V110_SDK71_' => true,
|
30
|
+
'_DEBUG' => true,
|
31
|
+
'_HAS_ITERATOR_DEBUGGING' => true,
|
32
|
+
'_SECURE_SCL' => true)
|
33
|
+
|
34
|
+
#=> true
|
15
35
|
```
|
36
|
+
|
data/lib/typespec.rb
CHANGED
@@ -1,2 +1,181 @@
|
|
1
|
+
# ...
|
1
2
|
module Typespec
|
3
|
+
# TODO(mtwilliams): Perform sanity checks.
|
4
|
+
# TODO(mtwilliams): Raise (custom) exceptions.
|
5
|
+
# TODO(mtwilliams): Explain the Type-Value-Or sysem.
|
6
|
+
# TODO(mtwilliams): Provide an example on how to rewrite exceptions.
|
7
|
+
# TODO(mtwilliams): Document this monstrosity.
|
8
|
+
|
9
|
+
# ...
|
10
|
+
def self.any; Typespec::Any; end
|
11
|
+
|
12
|
+
# ...
|
13
|
+
class Any
|
14
|
+
def self.valid?(_); true; end
|
15
|
+
end
|
16
|
+
|
17
|
+
# ...
|
18
|
+
def self.t; Typespec::Type; end
|
19
|
+
|
20
|
+
# ...
|
21
|
+
class Type
|
22
|
+
def initialize(type); @type = type; end
|
23
|
+
def self.[](type); self.new(type); end
|
24
|
+
def valid?(value); value.is_a?(@type); end
|
25
|
+
end
|
26
|
+
|
27
|
+
# ...
|
28
|
+
def self.expects; Typespec::Value; end
|
29
|
+
|
30
|
+
# ...
|
31
|
+
class Value
|
32
|
+
def initialize(value); @value = value; end
|
33
|
+
def self.[](value); self.new(value); end
|
34
|
+
def valid?(value) value.eql?(@value); end
|
35
|
+
end
|
36
|
+
|
37
|
+
# ...
|
38
|
+
def self.or; Typespec::Or; end
|
39
|
+
|
40
|
+
# ...
|
41
|
+
class Or
|
42
|
+
def initialize(expects); @expects = [*expects]; end
|
43
|
+
def valid?(value); @expects.any?{|expects| expects.valid?(value)}; end
|
44
|
+
end
|
45
|
+
|
46
|
+
# ...
|
47
|
+
def self.nil; Typespec.t[NilClass]; end
|
48
|
+
|
49
|
+
# ...
|
50
|
+
def self.boolean; Typespec.or[Typespec.t[TrueClass], Typespec.t[FalseClass]]; end
|
51
|
+
|
52
|
+
# ...
|
53
|
+
def self.number; Typespec.t[Numeric]; end
|
54
|
+
|
55
|
+
# ...
|
56
|
+
def self.integer; Typespec.t[Integer]; end
|
57
|
+
|
58
|
+
# ...
|
59
|
+
def self.float; Typespec.t[Float]; end
|
60
|
+
|
61
|
+
# ...
|
62
|
+
def self.rational; Typespec.t[Rational]; end
|
63
|
+
|
64
|
+
# ...
|
65
|
+
def self.string; Typespec.t[String]; end
|
66
|
+
|
67
|
+
# ...
|
68
|
+
def self.symbol; Typespec.t[Symbol]; end
|
69
|
+
|
70
|
+
# ...
|
71
|
+
def self.array; Typespec::Array; end
|
72
|
+
|
73
|
+
# ...
|
74
|
+
class Array
|
75
|
+
def initialize(*elements)
|
76
|
+
@element = Typespec.or[*elements]
|
77
|
+
end
|
78
|
+
|
79
|
+
def self.[](*elements)
|
80
|
+
Typespec::Array.new(*elements)
|
81
|
+
end
|
82
|
+
|
83
|
+
def valid?(value)
|
84
|
+
if value.is_a? Array
|
85
|
+
value.all?{|element| @element.valid?(element)}
|
86
|
+
else
|
87
|
+
false
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
# ...
|
93
|
+
def self.hash; Typespec::Hash; end
|
94
|
+
|
95
|
+
# ...
|
96
|
+
class Hash
|
97
|
+
def initialize(**pairs)
|
98
|
+
@pairs = pairs
|
99
|
+
@pairs ||= {Typespec.any => Typespec.any}
|
100
|
+
end
|
101
|
+
|
102
|
+
def self.[](**pairs)
|
103
|
+
Typespec::Hash.new(**pairs)
|
104
|
+
end
|
105
|
+
|
106
|
+
def valid?(value)
|
107
|
+
if value.is_a? Hash
|
108
|
+
value.all? do |k, v|
|
109
|
+
@pairs.any? do |pair|
|
110
|
+
pair.zip([k,v]).all? {|typespec, value| typespec.valid?(value)}
|
111
|
+
end
|
112
|
+
end
|
113
|
+
else
|
114
|
+
false
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
# ...
|
120
|
+
def self.struct; Typespec::Struct; end
|
121
|
+
|
122
|
+
# ...
|
123
|
+
class Struct
|
124
|
+
def initialize(*properties, **properties_with_spec)
|
125
|
+
if !properties.empty?
|
126
|
+
@properties = Hash[properties.map{|name| [name, Typespec.any]}]
|
127
|
+
elsif properties_with_spec
|
128
|
+
@properties = properties_with_spec
|
129
|
+
else
|
130
|
+
@properties = {}
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
def self.[](*properties, **properties_with_spec)
|
135
|
+
Typespec::Struct.new(*properties, **properties_with_spec)
|
136
|
+
end
|
137
|
+
|
138
|
+
def valid?(struct, opts={})
|
139
|
+
if struct.instance_of? Object
|
140
|
+
ignore_if_not_in_spec = opts.fetch(:ignore_if_not_in_spec, false)
|
141
|
+
struct.instance_variables.all? do |property|
|
142
|
+
undecorated = property.to_s[1..-1].to_sym
|
143
|
+
if @properties.include?(undecorated)
|
144
|
+
value = struct.instance_variable_get(property)
|
145
|
+
@properties[undecorated].valid?(value)
|
146
|
+
else
|
147
|
+
ignore_if_not_in_spec
|
148
|
+
end
|
149
|
+
end
|
150
|
+
else
|
151
|
+
false
|
152
|
+
end
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
# ...
|
157
|
+
def self.object; Typespec::Object; end
|
158
|
+
|
159
|
+
# ...
|
160
|
+
class Object < Struct
|
161
|
+
def self.[](*properties, **properties_with_spec)
|
162
|
+
Typespec::Object.new(*properties, **properties_with_spec)
|
163
|
+
end
|
164
|
+
|
165
|
+
def valid?(object, opts={})
|
166
|
+
super(object, opts.merge({:ignore_if_not_in_spec => true}))
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
# ...
|
171
|
+
def self.enum; Typespec::Enum; end
|
172
|
+
|
173
|
+
# ...
|
174
|
+
class Enum
|
175
|
+
def initialize(*can_take_on) @can_take_on = [*can_take_on] end
|
176
|
+
def self.[](*can_take_on); Typespec::Enum.new(can_take_on); end
|
177
|
+
def valid?(can_take_on) can_take_on.include?(@can_take_on); end
|
178
|
+
end
|
2
179
|
end
|
180
|
+
|
181
|
+
|
data/lib/typespec/gem.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: typespec
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michael Williams
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-11-
|
11
|
+
date: 2015-11-12 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Typespec is a way to specify complex schema made of types or specific
|
14
14
|
values and validate against them.
|