[ANN] Kwalify 0.7.0 - parser, schema validator, and data binding for YAML and JSON
"Makoto Kuwata" <[email protected]>
| Newsgroups | gmane.text.yaml.general |
|---|---|
| Message-ID | <[email protected]> |
Hi,
I have released Kwalify 0.7.0.
http://www.kuwata-lab.com/kwalify/
Kwalify is an integrated tool for YAML and JSON.
Kwalify contains parser, schema validator, and data binding tool.
This release has many enhancements and changes.
See
http://kuwata-lab.com/kwalify/ruby/CHANGES.txt
for details.
>From this release, Kwalify supports data binding.
If you specify class name in schema definition, Kwalify parses YAML/JSON
and create instance object of that class instead of Hash object.
Data binding makes handling YAML easier.
Kwalify requires Ruby1.8 or later (1.9 is not checked).
Enhancements:
- YAML parser is rewrited from scratch.
The new parser class Kwalify::Yaml::Parser is available.
The old parser class Kwalify::YamlParser is stil available,
but it is not recommended to use.
- Validator is integrated with yaml parser.
It is able to parse and validate at once.
--------------------
## create validator
require 'kwalify'
schema = Kwalify::Yaml.load_file('schema.yaml')
validator = Kwalify::Validator.new(schema)
## parse with validation
parser = Kwalify::Yaml::Parser.new(validator)
ydoc = parser.parse(File.read('data.yaml'))
## show validation errors if exist
errors = parser.errors()
if errors && !errors.empty?
for e in errors
puts "** %d:%d [%s] %s" % [e.linenum, e.column, e.path, e.message]
end
end
--------------------
- Data binding is integrated into Kwalify::Yaml::Parser.
If you set Kwalify::Yaml::Parser#data_binding to true
and you specified class names in schema file,
parser creates instance objects instead of Hash objects.
It means that you don't need to add '!ruby/Classname'
for each data.
.? schema file (config.schema.yaml)
--------------------
type: map
class: Config
mapping:
"host": { type: str, required: true }
"port": { type: int }
"user": { type: str, required: true }
"pass": { type: str, required: true }
--------------------
.? configuration file (config.yaml)
--------------------
host: localhost
port: 8080
user: user1
pass: password1
--------------------
.? ruby program (ex1.rb)
--------------------
## class definition
require 'kwalify/util/hashlike'
class Config
include Kwalify::Util::HashLike # defines [], []=, ...
attr_accessor :host, :posrt, :user, :pass
end
## create validator object
require 'kwalify'
schema = Kwalify::Yaml.load_file('config.schema.yaml')
validator = Kwalify::Validator.new(schema)
## parse configuration file with data binding
parser = Kwalify::Yaml::Parser.new(validator)
parser.data_binding = true # enable data binding
config = parser.parse_file('config.yaml')
p config #=> #<Config:0x542590 @user="user1", @port=8080,
# @pass="password1", @host="localhost">
--------------------
- Preceding alias supported.
If you set Kwalify::Yaml::Parser#preceding_alias to true,
parser allows aliases to apprear before corresponding anchor
appears.
This is very useful when node graph is complex.
--------------------
- name: Foo
parent: *bar # preceding alias
- &bar
name: Bar
parent: *baz # preceding alias
- &baz
name: Baz
--------------------
- New command-line option '-P' enables preceding alias.
- Kwalify::Yaml.load() and Kwalify::Yaml.load_file() are added.
They are similar to YAML.load() and YAML.load_file() but they
use Kwalify::Yaml::Parser object.
- New utilify method Kwalify::Util.traverse_schema() provided.
--------------------
require 'kwalify'
require 'kwalify/util'
schema = Kwalify::Yaml.load('schema.yaml')
Kwalify::Util.traverse_schema(schema) do |rulehash|
if classname = rulehash['class']
## add namespace to class name
rulehash['class'] = "Foo::Bar::#{classname}"
end
end
--------------------
- Add 'kwalify/util/hashlike.rb' which contains definition of
Kwalify::Util::HashLike module.
This module defines [], []=, keys(), key?(), and each() methods,
and these are required for data-binding.
- Action 'genclass-ruby' supports '--hashlike' property.
- Action 'genclass-ruby' supports '--initialize=false' property.
- Add action 'geclass-php'
- '\xXX' and '\uXXXX' are supported in Kwalify::Yaml::Parser.
- New constrant 'default:' is added to kwalify.schema.yaml.
This constrant have no effect to validation and parsing,
and it is used only in 'genclass-xxx' action.
Changes:
- Action 'genclass-ruby' and 'genclass-java' are changed to
generate boolean accessors.
For example, attribute 'active' is specified as 'type: bool'
in schema file, action 'genclass-ruby' generates
"def active? ; @active; end"
and action 'genclass-java' generates
"public boolean isActive() { return _active; }".
- Command-line option '-s' (silent) is obsolete and replaced with
'-q' (quiet). Option '-s' is still available but it is recommended
to use '-q'.
- License is changed from LGPL to MIT-LICENSE.
--
regards,
makoto kuwata
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/