Compare commits
11 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 73944804c1 | |||
| ffa189b885 | |||
| 08287da7b7 | |||
| 09093f729f | |||
| b78d0a5b80 | |||
| 38620ff9e2 | |||
| 18daaaa0d7 | |||
| 4ee669ebba | |||
| 32c1ed5c4b | |||
| d0a0d039c0 | |||
| de5d2bfb7d |
+15
@@ -0,0 +1,15 @@
|
|||||||
|
.eunit
|
||||||
|
deps
|
||||||
|
*.o
|
||||||
|
*.beam
|
||||||
|
*.plt
|
||||||
|
*.swp
|
||||||
|
erl_crash.dump
|
||||||
|
ebin/*.beam
|
||||||
|
doc/*.html
|
||||||
|
doc/*.css
|
||||||
|
doc/edoc-info
|
||||||
|
doc/erlang.png
|
||||||
|
rel/example_project
|
||||||
|
.concrete/DEV_MODE
|
||||||
|
.rebar
|
||||||
@@ -1,16 +1,674 @@
|
|||||||
ISC License
|
GNU GENERAL PUBLIC LICENSE
|
||||||
|
Version 3, 29 June 2007
|
||||||
|
|
||||||
Copyright (c) 2025, QPQ AG
|
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
|
||||||
Copyright (c) 2017, aeternity developers
|
Everyone is permitted to copy and distribute verbatim copies
|
||||||
|
of this license document, but changing it is not allowed.
|
||||||
|
|
||||||
Permission to use, copy, modify, and/or distribute this software for any
|
Preamble
|
||||||
purpose with or without fee is hereby granted, provided that the above
|
|
||||||
copyright notice and this permission notice appear in all copies.
|
|
||||||
|
|
||||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
The GNU General Public License is a free, copyleft license for
|
||||||
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
software and other kinds of works.
|
||||||
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
||||||
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
The licenses for most software and other practical works are designed
|
||||||
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
|
to take away your freedom to share and change the works. By contrast,
|
||||||
OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
the GNU General Public License is intended to guarantee your freedom to
|
||||||
PERFORMANCE OF THIS SOFTWARE.
|
share and change all versions of a program--to make sure it remains free
|
||||||
|
software for all its users. We, the Free Software Foundation, use the
|
||||||
|
GNU General Public License for most of our software; it applies also to
|
||||||
|
any other work released this way by its authors. You can apply it to
|
||||||
|
your programs, too.
|
||||||
|
|
||||||
|
When we speak of free software, we are referring to freedom, not
|
||||||
|
price. Our General Public Licenses are designed to make sure that you
|
||||||
|
have the freedom to distribute copies of free software (and charge for
|
||||||
|
them if you wish), that you receive source code or can get it if you
|
||||||
|
want it, that you can change the software or use pieces of it in new
|
||||||
|
free programs, and that you know you can do these things.
|
||||||
|
|
||||||
|
To protect your rights, we need to prevent others from denying you
|
||||||
|
these rights or asking you to surrender the rights. Therefore, you have
|
||||||
|
certain responsibilities if you distribute copies of the software, or if
|
||||||
|
you modify it: responsibilities to respect the freedom of others.
|
||||||
|
|
||||||
|
For example, if you distribute copies of such a program, whether
|
||||||
|
gratis or for a fee, you must pass on to the recipients the same
|
||||||
|
freedoms that you received. You must make sure that they, too, receive
|
||||||
|
or can get the source code. And you must show them these terms so they
|
||||||
|
know their rights.
|
||||||
|
|
||||||
|
Developers that use the GNU GPL protect your rights with two steps:
|
||||||
|
(1) assert copyright on the software, and (2) offer you this License
|
||||||
|
giving you legal permission to copy, distribute and/or modify it.
|
||||||
|
|
||||||
|
For the developers' and authors' protection, the GPL clearly explains
|
||||||
|
that there is no warranty for this free software. For both users' and
|
||||||
|
authors' sake, the GPL requires that modified versions be marked as
|
||||||
|
changed, so that their problems will not be attributed erroneously to
|
||||||
|
authors of previous versions.
|
||||||
|
|
||||||
|
Some devices are designed to deny users access to install or run
|
||||||
|
modified versions of the software inside them, although the manufacturer
|
||||||
|
can do so. This is fundamentally incompatible with the aim of
|
||||||
|
protecting users' freedom to change the software. The systematic
|
||||||
|
pattern of such abuse occurs in the area of products for individuals to
|
||||||
|
use, which is precisely where it is most unacceptable. Therefore, we
|
||||||
|
have designed this version of the GPL to prohibit the practice for those
|
||||||
|
products. If such problems arise substantially in other domains, we
|
||||||
|
stand ready to extend this provision to those domains in future versions
|
||||||
|
of the GPL, as needed to protect the freedom of users.
|
||||||
|
|
||||||
|
Finally, every program is threatened constantly by software patents.
|
||||||
|
States should not allow patents to restrict development and use of
|
||||||
|
software on general-purpose computers, but in those that do, we wish to
|
||||||
|
avoid the special danger that patents applied to a free program could
|
||||||
|
make it effectively proprietary. To prevent this, the GPL assures that
|
||||||
|
patents cannot be used to render the program non-free.
|
||||||
|
|
||||||
|
The precise terms and conditions for copying, distribution and
|
||||||
|
modification follow.
|
||||||
|
|
||||||
|
TERMS AND CONDITIONS
|
||||||
|
|
||||||
|
0. Definitions.
|
||||||
|
|
||||||
|
"This License" refers to version 3 of the GNU General Public License.
|
||||||
|
|
||||||
|
"Copyright" also means copyright-like laws that apply to other kinds of
|
||||||
|
works, such as semiconductor masks.
|
||||||
|
|
||||||
|
"The Program" refers to any copyrightable work licensed under this
|
||||||
|
License. Each licensee is addressed as "you". "Licensees" and
|
||||||
|
"recipients" may be individuals or organizations.
|
||||||
|
|
||||||
|
To "modify" a work means to copy from or adapt all or part of the work
|
||||||
|
in a fashion requiring copyright permission, other than the making of an
|
||||||
|
exact copy. The resulting work is called a "modified version" of the
|
||||||
|
earlier work or a work "based on" the earlier work.
|
||||||
|
|
||||||
|
A "covered work" means either the unmodified Program or a work based
|
||||||
|
on the Program.
|
||||||
|
|
||||||
|
To "propagate" a work means to do anything with it that, without
|
||||||
|
permission, would make you directly or secondarily liable for
|
||||||
|
infringement under applicable copyright law, except executing it on a
|
||||||
|
computer or modifying a private copy. Propagation includes copying,
|
||||||
|
distribution (with or without modification), making available to the
|
||||||
|
public, and in some countries other activities as well.
|
||||||
|
|
||||||
|
To "convey" a work means any kind of propagation that enables other
|
||||||
|
parties to make or receive copies. Mere interaction with a user through
|
||||||
|
a computer network, with no transfer of a copy, is not conveying.
|
||||||
|
|
||||||
|
An interactive user interface displays "Appropriate Legal Notices"
|
||||||
|
to the extent that it includes a convenient and prominently visible
|
||||||
|
feature that (1) displays an appropriate copyright notice, and (2)
|
||||||
|
tells the user that there is no warranty for the work (except to the
|
||||||
|
extent that warranties are provided), that licensees may convey the
|
||||||
|
work under this License, and how to view a copy of this License. If
|
||||||
|
the interface presents a list of user commands or options, such as a
|
||||||
|
menu, a prominent item in the list meets this criterion.
|
||||||
|
|
||||||
|
1. Source Code.
|
||||||
|
|
||||||
|
The "source code" for a work means the preferred form of the work
|
||||||
|
for making modifications to it. "Object code" means any non-source
|
||||||
|
form of a work.
|
||||||
|
|
||||||
|
A "Standard Interface" means an interface that either is an official
|
||||||
|
standard defined by a recognized standards body, or, in the case of
|
||||||
|
interfaces specified for a particular programming language, one that
|
||||||
|
is widely used among developers working in that language.
|
||||||
|
|
||||||
|
The "System Libraries" of an executable work include anything, other
|
||||||
|
than the work as a whole, that (a) is included in the normal form of
|
||||||
|
packaging a Major Component, but which is not part of that Major
|
||||||
|
Component, and (b) serves only to enable use of the work with that
|
||||||
|
Major Component, or to implement a Standard Interface for which an
|
||||||
|
implementation is available to the public in source code form. A
|
||||||
|
"Major Component", in this context, means a major essential component
|
||||||
|
(kernel, window system, and so on) of the specific operating system
|
||||||
|
(if any) on which the executable work runs, or a compiler used to
|
||||||
|
produce the work, or an object code interpreter used to run it.
|
||||||
|
|
||||||
|
The "Corresponding Source" for a work in object code form means all
|
||||||
|
the source code needed to generate, install, and (for an executable
|
||||||
|
work) run the object code and to modify the work, including scripts to
|
||||||
|
control those activities. However, it does not include the work's
|
||||||
|
System Libraries, or general-purpose tools or generally available free
|
||||||
|
programs which are used unmodified in performing those activities but
|
||||||
|
which are not part of the work. For example, Corresponding Source
|
||||||
|
includes interface definition files associated with source files for
|
||||||
|
the work, and the source code for shared libraries and dynamically
|
||||||
|
linked subprograms that the work is specifically designed to require,
|
||||||
|
such as by intimate data communication or control flow between those
|
||||||
|
subprograms and other parts of the work.
|
||||||
|
|
||||||
|
The Corresponding Source need not include anything that users
|
||||||
|
can regenerate automatically from other parts of the Corresponding
|
||||||
|
Source.
|
||||||
|
|
||||||
|
The Corresponding Source for a work in source code form is that
|
||||||
|
same work.
|
||||||
|
|
||||||
|
2. Basic Permissions.
|
||||||
|
|
||||||
|
All rights granted under this License are granted for the term of
|
||||||
|
copyright on the Program, and are irrevocable provided the stated
|
||||||
|
conditions are met. This License explicitly affirms your unlimited
|
||||||
|
permission to run the unmodified Program. The output from running a
|
||||||
|
covered work is covered by this License only if the output, given its
|
||||||
|
content, constitutes a covered work. This License acknowledges your
|
||||||
|
rights of fair use or other equivalent, as provided by copyright law.
|
||||||
|
|
||||||
|
You may make, run and propagate covered works that you do not
|
||||||
|
convey, without conditions so long as your license otherwise remains
|
||||||
|
in force. You may convey covered works to others for the sole purpose
|
||||||
|
of having them make modifications exclusively for you, or provide you
|
||||||
|
with facilities for running those works, provided that you comply with
|
||||||
|
the terms of this License in conveying all material for which you do
|
||||||
|
not control copyright. Those thus making or running the covered works
|
||||||
|
for you must do so exclusively on your behalf, under your direction
|
||||||
|
and control, on terms that prohibit them from making any copies of
|
||||||
|
your copyrighted material outside their relationship with you.
|
||||||
|
|
||||||
|
Conveying under any other circumstances is permitted solely under
|
||||||
|
the conditions stated below. Sublicensing is not allowed; section 10
|
||||||
|
makes it unnecessary.
|
||||||
|
|
||||||
|
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
|
||||||
|
|
||||||
|
No covered work shall be deemed part of an effective technological
|
||||||
|
measure under any applicable law fulfilling obligations under article
|
||||||
|
11 of the WIPO copyright treaty adopted on 20 December 1996, or
|
||||||
|
similar laws prohibiting or restricting circumvention of such
|
||||||
|
measures.
|
||||||
|
|
||||||
|
When you convey a covered work, you waive any legal power to forbid
|
||||||
|
circumvention of technological measures to the extent such circumvention
|
||||||
|
is effected by exercising rights under this License with respect to
|
||||||
|
the covered work, and you disclaim any intention to limit operation or
|
||||||
|
modification of the work as a means of enforcing, against the work's
|
||||||
|
users, your or third parties' legal rights to forbid circumvention of
|
||||||
|
technological measures.
|
||||||
|
|
||||||
|
4. Conveying Verbatim Copies.
|
||||||
|
|
||||||
|
You may convey verbatim copies of the Program's source code as you
|
||||||
|
receive it, in any medium, provided that you conspicuously and
|
||||||
|
appropriately publish on each copy an appropriate copyright notice;
|
||||||
|
keep intact all notices stating that this License and any
|
||||||
|
non-permissive terms added in accord with section 7 apply to the code;
|
||||||
|
keep intact all notices of the absence of any warranty; and give all
|
||||||
|
recipients a copy of this License along with the Program.
|
||||||
|
|
||||||
|
You may charge any price or no price for each copy that you convey,
|
||||||
|
and you may offer support or warranty protection for a fee.
|
||||||
|
|
||||||
|
5. Conveying Modified Source Versions.
|
||||||
|
|
||||||
|
You may convey a work based on the Program, or the modifications to
|
||||||
|
produce it from the Program, in the form of source code under the
|
||||||
|
terms of section 4, provided that you also meet all of these conditions:
|
||||||
|
|
||||||
|
a) The work must carry prominent notices stating that you modified
|
||||||
|
it, and giving a relevant date.
|
||||||
|
|
||||||
|
b) The work must carry prominent notices stating that it is
|
||||||
|
released under this License and any conditions added under section
|
||||||
|
7. This requirement modifies the requirement in section 4 to
|
||||||
|
"keep intact all notices".
|
||||||
|
|
||||||
|
c) You must license the entire work, as a whole, under this
|
||||||
|
License to anyone who comes into possession of a copy. This
|
||||||
|
License will therefore apply, along with any applicable section 7
|
||||||
|
additional terms, to the whole of the work, and all its parts,
|
||||||
|
regardless of how they are packaged. This License gives no
|
||||||
|
permission to license the work in any other way, but it does not
|
||||||
|
invalidate such permission if you have separately received it.
|
||||||
|
|
||||||
|
d) If the work has interactive user interfaces, each must display
|
||||||
|
Appropriate Legal Notices; however, if the Program has interactive
|
||||||
|
interfaces that do not display Appropriate Legal Notices, your
|
||||||
|
work need not make them do so.
|
||||||
|
|
||||||
|
A compilation of a covered work with other separate and independent
|
||||||
|
works, which are not by their nature extensions of the covered work,
|
||||||
|
and which are not combined with it such as to form a larger program,
|
||||||
|
in or on a volume of a storage or distribution medium, is called an
|
||||||
|
"aggregate" if the compilation and its resulting copyright are not
|
||||||
|
used to limit the access or legal rights of the compilation's users
|
||||||
|
beyond what the individual works permit. Inclusion of a covered work
|
||||||
|
in an aggregate does not cause this License to apply to the other
|
||||||
|
parts of the aggregate.
|
||||||
|
|
||||||
|
6. Conveying Non-Source Forms.
|
||||||
|
|
||||||
|
You may convey a covered work in object code form under the terms
|
||||||
|
of sections 4 and 5, provided that you also convey the
|
||||||
|
machine-readable Corresponding Source under the terms of this License,
|
||||||
|
in one of these ways:
|
||||||
|
|
||||||
|
a) Convey the object code in, or embodied in, a physical product
|
||||||
|
(including a physical distribution medium), accompanied by the
|
||||||
|
Corresponding Source fixed on a durable physical medium
|
||||||
|
customarily used for software interchange.
|
||||||
|
|
||||||
|
b) Convey the object code in, or embodied in, a physical product
|
||||||
|
(including a physical distribution medium), accompanied by a
|
||||||
|
written offer, valid for at least three years and valid for as
|
||||||
|
long as you offer spare parts or customer support for that product
|
||||||
|
model, to give anyone who possesses the object code either (1) a
|
||||||
|
copy of the Corresponding Source for all the software in the
|
||||||
|
product that is covered by this License, on a durable physical
|
||||||
|
medium customarily used for software interchange, for a price no
|
||||||
|
more than your reasonable cost of physically performing this
|
||||||
|
conveying of source, or (2) access to copy the
|
||||||
|
Corresponding Source from a network server at no charge.
|
||||||
|
|
||||||
|
c) Convey individual copies of the object code with a copy of the
|
||||||
|
written offer to provide the Corresponding Source. This
|
||||||
|
alternative is allowed only occasionally and noncommercially, and
|
||||||
|
only if you received the object code with such an offer, in accord
|
||||||
|
with subsection 6b.
|
||||||
|
|
||||||
|
d) Convey the object code by offering access from a designated
|
||||||
|
place (gratis or for a charge), and offer equivalent access to the
|
||||||
|
Corresponding Source in the same way through the same place at no
|
||||||
|
further charge. You need not require recipients to copy the
|
||||||
|
Corresponding Source along with the object code. If the place to
|
||||||
|
copy the object code is a network server, the Corresponding Source
|
||||||
|
may be on a different server (operated by you or a third party)
|
||||||
|
that supports equivalent copying facilities, provided you maintain
|
||||||
|
clear directions next to the object code saying where to find the
|
||||||
|
Corresponding Source. Regardless of what server hosts the
|
||||||
|
Corresponding Source, you remain obligated to ensure that it is
|
||||||
|
available for as long as needed to satisfy these requirements.
|
||||||
|
|
||||||
|
e) Convey the object code using peer-to-peer transmission, provided
|
||||||
|
you inform other peers where the object code and Corresponding
|
||||||
|
Source of the work are being offered to the general public at no
|
||||||
|
charge under subsection 6d.
|
||||||
|
|
||||||
|
A separable portion of the object code, whose source code is excluded
|
||||||
|
from the Corresponding Source as a System Library, need not be
|
||||||
|
included in conveying the object code work.
|
||||||
|
|
||||||
|
A "User Product" is either (1) a "consumer product", which means any
|
||||||
|
tangible personal property which is normally used for personal, family,
|
||||||
|
or household purposes, or (2) anything designed or sold for incorporation
|
||||||
|
into a dwelling. In determining whether a product is a consumer product,
|
||||||
|
doubtful cases shall be resolved in favor of coverage. For a particular
|
||||||
|
product received by a particular user, "normally used" refers to a
|
||||||
|
typical or common use of that class of product, regardless of the status
|
||||||
|
of the particular user or of the way in which the particular user
|
||||||
|
actually uses, or expects or is expected to use, the product. A product
|
||||||
|
is a consumer product regardless of whether the product has substantial
|
||||||
|
commercial, industrial or non-consumer uses, unless such uses represent
|
||||||
|
the only significant mode of use of the product.
|
||||||
|
|
||||||
|
"Installation Information" for a User Product means any methods,
|
||||||
|
procedures, authorization keys, or other information required to install
|
||||||
|
and execute modified versions of a covered work in that User Product from
|
||||||
|
a modified version of its Corresponding Source. The information must
|
||||||
|
suffice to ensure that the continued functioning of the modified object
|
||||||
|
code is in no case prevented or interfered with solely because
|
||||||
|
modification has been made.
|
||||||
|
|
||||||
|
If you convey an object code work under this section in, or with, or
|
||||||
|
specifically for use in, a User Product, and the conveying occurs as
|
||||||
|
part of a transaction in which the right of possession and use of the
|
||||||
|
User Product is transferred to the recipient in perpetuity or for a
|
||||||
|
fixed term (regardless of how the transaction is characterized), the
|
||||||
|
Corresponding Source conveyed under this section must be accompanied
|
||||||
|
by the Installation Information. But this requirement does not apply
|
||||||
|
if neither you nor any third party retains the ability to install
|
||||||
|
modified object code on the User Product (for example, the work has
|
||||||
|
been installed in ROM).
|
||||||
|
|
||||||
|
The requirement to provide Installation Information does not include a
|
||||||
|
requirement to continue to provide support service, warranty, or updates
|
||||||
|
for a work that has been modified or installed by the recipient, or for
|
||||||
|
the User Product in which it has been modified or installed. Access to a
|
||||||
|
network may be denied when the modification itself materially and
|
||||||
|
adversely affects the operation of the network or violates the rules and
|
||||||
|
protocols for communication across the network.
|
||||||
|
|
||||||
|
Corresponding Source conveyed, and Installation Information provided,
|
||||||
|
in accord with this section must be in a format that is publicly
|
||||||
|
documented (and with an implementation available to the public in
|
||||||
|
source code form), and must require no special password or key for
|
||||||
|
unpacking, reading or copying.
|
||||||
|
|
||||||
|
7. Additional Terms.
|
||||||
|
|
||||||
|
"Additional permissions" are terms that supplement the terms of this
|
||||||
|
License by making exceptions from one or more of its conditions.
|
||||||
|
Additional permissions that are applicable to the entire Program shall
|
||||||
|
be treated as though they were included in this License, to the extent
|
||||||
|
that they are valid under applicable law. If additional permissions
|
||||||
|
apply only to part of the Program, that part may be used separately
|
||||||
|
under those permissions, but the entire Program remains governed by
|
||||||
|
this License without regard to the additional permissions.
|
||||||
|
|
||||||
|
When you convey a copy of a covered work, you may at your option
|
||||||
|
remove any additional permissions from that copy, or from any part of
|
||||||
|
it. (Additional permissions may be written to require their own
|
||||||
|
removal in certain cases when you modify the work.) You may place
|
||||||
|
additional permissions on material, added by you to a covered work,
|
||||||
|
for which you have or can give appropriate copyright permission.
|
||||||
|
|
||||||
|
Notwithstanding any other provision of this License, for material you
|
||||||
|
add to a covered work, you may (if authorized by the copyright holders of
|
||||||
|
that material) supplement the terms of this License with terms:
|
||||||
|
|
||||||
|
a) Disclaiming warranty or limiting liability differently from the
|
||||||
|
terms of sections 15 and 16 of this License; or
|
||||||
|
|
||||||
|
b) Requiring preservation of specified reasonable legal notices or
|
||||||
|
author attributions in that material or in the Appropriate Legal
|
||||||
|
Notices displayed by works containing it; or
|
||||||
|
|
||||||
|
c) Prohibiting misrepresentation of the origin of that material, or
|
||||||
|
requiring that modified versions of such material be marked in
|
||||||
|
reasonable ways as different from the original version; or
|
||||||
|
|
||||||
|
d) Limiting the use for publicity purposes of names of licensors or
|
||||||
|
authors of the material; or
|
||||||
|
|
||||||
|
e) Declining to grant rights under trademark law for use of some
|
||||||
|
trade names, trademarks, or service marks; or
|
||||||
|
|
||||||
|
f) Requiring indemnification of licensors and authors of that
|
||||||
|
material by anyone who conveys the material (or modified versions of
|
||||||
|
it) with contractual assumptions of liability to the recipient, for
|
||||||
|
any liability that these contractual assumptions directly impose on
|
||||||
|
those licensors and authors.
|
||||||
|
|
||||||
|
All other non-permissive additional terms are considered "further
|
||||||
|
restrictions" within the meaning of section 10. If the Program as you
|
||||||
|
received it, or any part of it, contains a notice stating that it is
|
||||||
|
governed by this License along with a term that is a further
|
||||||
|
restriction, you may remove that term. If a license document contains
|
||||||
|
a further restriction but permits relicensing or conveying under this
|
||||||
|
License, you may add to a covered work material governed by the terms
|
||||||
|
of that license document, provided that the further restriction does
|
||||||
|
not survive such relicensing or conveying.
|
||||||
|
|
||||||
|
If you add terms to a covered work in accord with this section, you
|
||||||
|
must place, in the relevant source files, a statement of the
|
||||||
|
additional terms that apply to those files, or a notice indicating
|
||||||
|
where to find the applicable terms.
|
||||||
|
|
||||||
|
Additional terms, permissive or non-permissive, may be stated in the
|
||||||
|
form of a separately written license, or stated as exceptions;
|
||||||
|
the above requirements apply either way.
|
||||||
|
|
||||||
|
8. Termination.
|
||||||
|
|
||||||
|
You may not propagate or modify a covered work except as expressly
|
||||||
|
provided under this License. Any attempt otherwise to propagate or
|
||||||
|
modify it is void, and will automatically terminate your rights under
|
||||||
|
this License (including any patent licenses granted under the third
|
||||||
|
paragraph of section 11).
|
||||||
|
|
||||||
|
However, if you cease all violation of this License, then your
|
||||||
|
license from a particular copyright holder is reinstated (a)
|
||||||
|
provisionally, unless and until the copyright holder explicitly and
|
||||||
|
finally terminates your license, and (b) permanently, if the copyright
|
||||||
|
holder fails to notify you of the violation by some reasonable means
|
||||||
|
prior to 60 days after the cessation.
|
||||||
|
|
||||||
|
Moreover, your license from a particular copyright holder is
|
||||||
|
reinstated permanently if the copyright holder notifies you of the
|
||||||
|
violation by some reasonable means, this is the first time you have
|
||||||
|
received notice of violation of this License (for any work) from that
|
||||||
|
copyright holder, and you cure the violation prior to 30 days after
|
||||||
|
your receipt of the notice.
|
||||||
|
|
||||||
|
Termination of your rights under this section does not terminate the
|
||||||
|
licenses of parties who have received copies or rights from you under
|
||||||
|
this License. If your rights have been terminated and not permanently
|
||||||
|
reinstated, you do not qualify to receive new licenses for the same
|
||||||
|
material under section 10.
|
||||||
|
|
||||||
|
9. Acceptance Not Required for Having Copies.
|
||||||
|
|
||||||
|
You are not required to accept this License in order to receive or
|
||||||
|
run a copy of the Program. Ancillary propagation of a covered work
|
||||||
|
occurring solely as a consequence of using peer-to-peer transmission
|
||||||
|
to receive a copy likewise does not require acceptance. However,
|
||||||
|
nothing other than this License grants you permission to propagate or
|
||||||
|
modify any covered work. These actions infringe copyright if you do
|
||||||
|
not accept this License. Therefore, by modifying or propagating a
|
||||||
|
covered work, you indicate your acceptance of this License to do so.
|
||||||
|
|
||||||
|
10. Automatic Licensing of Downstream Recipients.
|
||||||
|
|
||||||
|
Each time you convey a covered work, the recipient automatically
|
||||||
|
receives a license from the original licensors, to run, modify and
|
||||||
|
propagate that work, subject to this License. You are not responsible
|
||||||
|
for enforcing compliance by third parties with this License.
|
||||||
|
|
||||||
|
An "entity transaction" is a transaction transferring control of an
|
||||||
|
organization, or substantially all assets of one, or subdividing an
|
||||||
|
organization, or merging organizations. If propagation of a covered
|
||||||
|
work results from an entity transaction, each party to that
|
||||||
|
transaction who receives a copy of the work also receives whatever
|
||||||
|
licenses to the work the party's predecessor in interest had or could
|
||||||
|
give under the previous paragraph, plus a right to possession of the
|
||||||
|
Corresponding Source of the work from the predecessor in interest, if
|
||||||
|
the predecessor has it or can get it with reasonable efforts.
|
||||||
|
|
||||||
|
You may not impose any further restrictions on the exercise of the
|
||||||
|
rights granted or affirmed under this License. For example, you may
|
||||||
|
not impose a license fee, royalty, or other charge for exercise of
|
||||||
|
rights granted under this License, and you may not initiate litigation
|
||||||
|
(including a cross-claim or counterclaim in a lawsuit) alleging that
|
||||||
|
any patent claim is infringed by making, using, selling, offering for
|
||||||
|
sale, or importing the Program or any portion of it.
|
||||||
|
|
||||||
|
11. Patents.
|
||||||
|
|
||||||
|
A "contributor" is a copyright holder who authorizes use under this
|
||||||
|
License of the Program or a work on which the Program is based. The
|
||||||
|
work thus licensed is called the contributor's "contributor version".
|
||||||
|
|
||||||
|
A contributor's "essential patent claims" are all patent claims
|
||||||
|
owned or controlled by the contributor, whether already acquired or
|
||||||
|
hereafter acquired, that would be infringed by some manner, permitted
|
||||||
|
by this License, of making, using, or selling its contributor version,
|
||||||
|
but do not include claims that would be infringed only as a
|
||||||
|
consequence of further modification of the contributor version. For
|
||||||
|
purposes of this definition, "control" includes the right to grant
|
||||||
|
patent sublicenses in a manner consistent with the requirements of
|
||||||
|
this License.
|
||||||
|
|
||||||
|
Each contributor grants you a non-exclusive, worldwide, royalty-free
|
||||||
|
patent license under the contributor's essential patent claims, to
|
||||||
|
make, use, sell, offer for sale, import and otherwise run, modify and
|
||||||
|
propagate the contents of its contributor version.
|
||||||
|
|
||||||
|
In the following three paragraphs, a "patent license" is any express
|
||||||
|
agreement or commitment, however denominated, not to enforce a patent
|
||||||
|
(such as an express permission to practice a patent or covenant not to
|
||||||
|
sue for patent infringement). To "grant" such a patent license to a
|
||||||
|
party means to make such an agreement or commitment not to enforce a
|
||||||
|
patent against the party.
|
||||||
|
|
||||||
|
If you convey a covered work, knowingly relying on a patent license,
|
||||||
|
and the Corresponding Source of the work is not available for anyone
|
||||||
|
to copy, free of charge and under the terms of this License, through a
|
||||||
|
publicly available network server or other readily accessible means,
|
||||||
|
then you must either (1) cause the Corresponding Source to be so
|
||||||
|
available, or (2) arrange to deprive yourself of the benefit of the
|
||||||
|
patent license for this particular work, or (3) arrange, in a manner
|
||||||
|
consistent with the requirements of this License, to extend the patent
|
||||||
|
license to downstream recipients. "Knowingly relying" means you have
|
||||||
|
actual knowledge that, but for the patent license, your conveying the
|
||||||
|
covered work in a country, or your recipient's use of the covered work
|
||||||
|
in a country, would infringe one or more identifiable patents in that
|
||||||
|
country that you have reason to believe are valid.
|
||||||
|
|
||||||
|
If, pursuant to or in connection with a single transaction or
|
||||||
|
arrangement, you convey, or propagate by procuring conveyance of, a
|
||||||
|
covered work, and grant a patent license to some of the parties
|
||||||
|
receiving the covered work authorizing them to use, propagate, modify
|
||||||
|
or convey a specific copy of the covered work, then the patent license
|
||||||
|
you grant is automatically extended to all recipients of the covered
|
||||||
|
work and works based on it.
|
||||||
|
|
||||||
|
A patent license is "discriminatory" if it does not include within
|
||||||
|
the scope of its coverage, prohibits the exercise of, or is
|
||||||
|
conditioned on the non-exercise of one or more of the rights that are
|
||||||
|
specifically granted under this License. You may not convey a covered
|
||||||
|
work if you are a party to an arrangement with a third party that is
|
||||||
|
in the business of distributing software, under which you make payment
|
||||||
|
to the third party based on the extent of your activity of conveying
|
||||||
|
the work, and under which the third party grants, to any of the
|
||||||
|
parties who would receive the covered work from you, a discriminatory
|
||||||
|
patent license (a) in connection with copies of the covered work
|
||||||
|
conveyed by you (or copies made from those copies), or (b) primarily
|
||||||
|
for and in connection with specific products or compilations that
|
||||||
|
contain the covered work, unless you entered into that arrangement,
|
||||||
|
or that patent license was granted, prior to 28 March 2007.
|
||||||
|
|
||||||
|
Nothing in this License shall be construed as excluding or limiting
|
||||||
|
any implied license or other defenses to infringement that may
|
||||||
|
otherwise be available to you under applicable patent law.
|
||||||
|
|
||||||
|
12. No Surrender of Others' Freedom.
|
||||||
|
|
||||||
|
If conditions are imposed on you (whether by court order, agreement or
|
||||||
|
otherwise) that contradict the conditions of this License, they do not
|
||||||
|
excuse you from the conditions of this License. If you cannot convey a
|
||||||
|
covered work so as to satisfy simultaneously your obligations under this
|
||||||
|
License and any other pertinent obligations, then as a consequence you may
|
||||||
|
not convey it at all. For example, if you agree to terms that obligate you
|
||||||
|
to collect a royalty for further conveying from those to whom you convey
|
||||||
|
the Program, the only way you could satisfy both those terms and this
|
||||||
|
License would be to refrain entirely from conveying the Program.
|
||||||
|
|
||||||
|
13. Use with the GNU Affero General Public License.
|
||||||
|
|
||||||
|
Notwithstanding any other provision of this License, you have
|
||||||
|
permission to link or combine any covered work with a work licensed
|
||||||
|
under version 3 of the GNU Affero General Public License into a single
|
||||||
|
combined work, and to convey the resulting work. The terms of this
|
||||||
|
License will continue to apply to the part which is the covered work,
|
||||||
|
but the special requirements of the GNU Affero General Public License,
|
||||||
|
section 13, concerning interaction through a network will apply to the
|
||||||
|
combination as such.
|
||||||
|
|
||||||
|
14. Revised Versions of this License.
|
||||||
|
|
||||||
|
The Free Software Foundation may publish revised and/or new versions of
|
||||||
|
the GNU General Public License from time to time. Such new versions will
|
||||||
|
be similar in spirit to the present version, but may differ in detail to
|
||||||
|
address new problems or concerns.
|
||||||
|
|
||||||
|
Each version is given a distinguishing version number. If the
|
||||||
|
Program specifies that a certain numbered version of the GNU General
|
||||||
|
Public License "or any later version" applies to it, you have the
|
||||||
|
option of following the terms and conditions either of that numbered
|
||||||
|
version or of any later version published by the Free Software
|
||||||
|
Foundation. If the Program does not specify a version number of the
|
||||||
|
GNU General Public License, you may choose any version ever published
|
||||||
|
by the Free Software Foundation.
|
||||||
|
|
||||||
|
If the Program specifies that a proxy can decide which future
|
||||||
|
versions of the GNU General Public License can be used, that proxy's
|
||||||
|
public statement of acceptance of a version permanently authorizes you
|
||||||
|
to choose that version for the Program.
|
||||||
|
|
||||||
|
Later license versions may give you additional or different
|
||||||
|
permissions. However, no additional obligations are imposed on any
|
||||||
|
author or copyright holder as a result of your choosing to follow a
|
||||||
|
later version.
|
||||||
|
|
||||||
|
15. Disclaimer of Warranty.
|
||||||
|
|
||||||
|
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
|
||||||
|
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
|
||||||
|
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
|
||||||
|
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
|
||||||
|
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||||
|
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
|
||||||
|
IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
|
||||||
|
ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
|
||||||
|
|
||||||
|
16. Limitation of Liability.
|
||||||
|
|
||||||
|
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
|
||||||
|
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
|
||||||
|
THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
|
||||||
|
GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
|
||||||
|
USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
|
||||||
|
DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
|
||||||
|
PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
|
||||||
|
EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
|
||||||
|
SUCH DAMAGES.
|
||||||
|
|
||||||
|
17. Interpretation of Sections 15 and 16.
|
||||||
|
|
||||||
|
If the disclaimer of warranty and limitation of liability provided
|
||||||
|
above cannot be given local legal effect according to their terms,
|
||||||
|
reviewing courts shall apply local law that most closely approximates
|
||||||
|
an absolute waiver of all civil liability in connection with the
|
||||||
|
Program, unless a warranty or assumption of liability accompanies a
|
||||||
|
copy of the Program in return for a fee.
|
||||||
|
|
||||||
|
END OF TERMS AND CONDITIONS
|
||||||
|
|
||||||
|
How to Apply These Terms to Your New Programs
|
||||||
|
|
||||||
|
If you develop a new program, and you want it to be of the greatest
|
||||||
|
possible use to the public, the best way to achieve this is to make it
|
||||||
|
free software which everyone can redistribute and change under these terms.
|
||||||
|
|
||||||
|
To do so, attach the following notices to the program. It is safest
|
||||||
|
to attach them to the start of each source file to most effectively
|
||||||
|
state the exclusion of warranty; and each file should have at least
|
||||||
|
the "copyright" line and a pointer to where the full notice is found.
|
||||||
|
|
||||||
|
<one line to give the program's name and a brief idea of what it does.>
|
||||||
|
Copyright (C) <year> <name of author>
|
||||||
|
|
||||||
|
This program is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU General Public License as published by
|
||||||
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License
|
||||||
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
Also add information on how to contact you by electronic and paper mail.
|
||||||
|
|
||||||
|
If the program does terminal interaction, make it output a short
|
||||||
|
notice like this when it starts in an interactive mode:
|
||||||
|
|
||||||
|
<program> Copyright (C) <year> <name of author>
|
||||||
|
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
|
||||||
|
This is free software, and you are welcome to redistribute it
|
||||||
|
under certain conditions; type `show c' for details.
|
||||||
|
|
||||||
|
The hypothetical commands `show w' and `show c' should show the appropriate
|
||||||
|
parts of the General Public License. Of course, your program's commands
|
||||||
|
might be different; for a GUI interface, you would use an "about box".
|
||||||
|
|
||||||
|
You should also get your employer (if you work as a programmer) or school,
|
||||||
|
if any, to sign a "copyright disclaimer" for the program, if necessary.
|
||||||
|
For more information on this, and how to apply and follow the GNU GPL, see
|
||||||
|
<https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
The GNU General Public License does not permit incorporating your program
|
||||||
|
into proprietary programs. If your program is a subroutine library, you
|
||||||
|
may consider it more useful to permit linking proprietary applications with
|
||||||
|
the library. If this is what you want to do, use the GNU Lesser General
|
||||||
|
Public License instead of this License. But first, please read
|
||||||
|
<https://www.gnu.org/licenses/why-not-lgpl.html>.
|
||||||
|
|||||||
@@ -11,6 +11,9 @@ management subsystem. It is based on JSON-Schema, and includes, among other thin
|
|||||||
* Caching of the user config (and schema) as persistent terms
|
* Caching of the user config (and schema) as persistent terms
|
||||||
* Fast config lookups using key paths
|
* Fast config lookups using key paths
|
||||||
* Lookups can handle both schema defaults and user-provided defaults
|
* Lookups can handle both schema defaults and user-provided defaults
|
||||||
|
* Optional type coercion during validation
|
||||||
|
* Optional conversion of enums to atoms
|
||||||
|
* Optional extensions with custom validator funs
|
||||||
|
|
||||||
## JSON-Schema validator
|
## JSON-Schema validator
|
||||||
|
|
||||||
@@ -78,3 +81,63 @@ there are surely other things that are unsupported.
|
|||||||
* `"merge"` (for objects, keeps and/or updates existing values)
|
* `"merge"` (for objects, keeps and/or updates existing values)
|
||||||
* `"suggest"` (adds value if not already present)
|
* `"suggest"` (adds value if not already present)
|
||||||
|
|
||||||
|
### Custom validation options
|
||||||
|
|
||||||
|
Using `gmconfig_schema_utils:validate(Json, Schema, Opts)`, a few options are supported
|
||||||
|
to further enhance the validation (`Opts` is of type `map()`):
|
||||||
|
|
||||||
|
`coerce => boolean()` converts strings to integers, null and booleans when needed.
|
||||||
|
`enums_to_atoms => boolean()` converts enum strings to atoms
|
||||||
|
`extensions => map()` supports mapping `x-...` properties to custom validators.
|
||||||
|
|
||||||
|
#### Validator extensions
|
||||||
|
|
||||||
|
See the following test case:
|
||||||
|
```erlang
|
||||||
|
t_nested_refs() ->
|
||||||
|
S = read("data/nested_refs_schema.json"),
|
||||||
|
F = fun(Str, #{<<"tags">> := Tags}) ->
|
||||||
|
true = lists:any(
|
||||||
|
fun(T) ->
|
||||||
|
nomatch =/= string:prefix(Str, T)
|
||||||
|
end, Tags)
|
||||||
|
end,
|
||||||
|
Opts = #{extensions => #{<<"x-serialization">> => F}},
|
||||||
|
Vs = #{<<"tx">> => #{<<"from">> => <<"ak_good">>}},
|
||||||
|
Vf = #{<<"tx">> => #{<<"from">> => <<"ac_bad">>}},
|
||||||
|
validate(Vs, S, Opts),
|
||||||
|
fails(Vf, S, Opts, #{e => failing_schemas}),
|
||||||
|
ok.
|
||||||
|
```
|
||||||
|
This simulates an encoding extension, where the example fun here simply checks
|
||||||
|
if tags specified under the `x-serialization` property are prefixes of the
|
||||||
|
given string value.
|
||||||
|
|
||||||
|
In the test schema, we can see the following definition:
|
||||||
|
```json
|
||||||
|
"properties": {
|
||||||
|
"from": {
|
||||||
|
"allOf": [
|
||||||
|
{ "$ref": "#/components/schemas/Pubkey" },
|
||||||
|
{ "x-serialization": {
|
||||||
|
"tags": ["ak"]
|
||||||
|
}}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
...
|
||||||
|
"Pubkey": {
|
||||||
|
"type": "string",
|
||||||
|
"x-serialization": {
|
||||||
|
"tags": ["ak", "ct"]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Whenever the validator encounters an `x-...` property mapped to a validator fun,
|
||||||
|
this fun is called with the value and the schema part of the property. The return
|
||||||
|
value of the fun is ignored, and any normal return is treated as a validation success.
|
||||||
|
|
||||||
|
The example illustrates a common pattern in OpenAPI specs, where entity references are
|
||||||
|
used extensively. The `Pubkey` data type can have a more general `x-serialization`
|
||||||
|
definition, where multiple key types are accepted, whereas a specialized use of the
|
||||||
|
type can narrow the scope by accepting only a subset of the possible types.
|
||||||
|
|||||||
@@ -0,0 +1,11 @@
|
|||||||
|
{application,gmconfig,
|
||||||
|
[{description,"Configuration management support"},
|
||||||
|
{vsn,"0.2.0"},
|
||||||
|
{registered,[]},
|
||||||
|
{applications,[kernel,stdlib,setup]},
|
||||||
|
{env,[]},
|
||||||
|
{modules,[gmconfig,gmconfig_schema_helpers,
|
||||||
|
gmconfig_schema_utils]},
|
||||||
|
{maintainers,["QPQ IaaS AG"]},
|
||||||
|
{licensens,["ISC"]},
|
||||||
|
{links,[{"gitea","https://git.qpq.swiss/gmconfig"}]}]}.
|
||||||
+2
-4
@@ -3,9 +3,7 @@
|
|||||||
{plugins, [rebar3_hex]}.
|
{plugins, [rebar3_hex]}.
|
||||||
|
|
||||||
{deps, [
|
{deps, [
|
||||||
{zj, {git, "https://gitlab.com/zxq9/zj.git", {tag, "1.1.2"}}}
|
{setup, "2.2.1"}
|
||||||
, {yamerl, "0.10.0"}
|
|
||||||
, {setup, "2.1.2"}
|
|
||||||
]}.
|
]}.
|
||||||
|
|
||||||
{profiles, [
|
{profiles, [
|
||||||
@@ -17,5 +15,5 @@
|
|||||||
deprecated_function_calls, deprecated_functions]}.
|
deprecated_function_calls, deprecated_functions]}.
|
||||||
|
|
||||||
{dialyzer, [ {warnings, [unknown]}
|
{dialyzer, [ {warnings, [unknown]}
|
||||||
, {base_plt_apps, [erts, kernel, stdlib, yamerl, zj, setup]}
|
, {base_plt_apps, [erts, kernel, stdlib, setup]}
|
||||||
]}.
|
]}.
|
||||||
|
|||||||
+4
-15
@@ -1,15 +1,4 @@
|
|||||||
{"1.2.0",
|
[{<<"setup">>,
|
||||||
[{<<"setup">>,{pkg,<<"setup">>,<<"2.1.2">>},0},
|
{git,"https://github.com/uwiger/setup.git",
|
||||||
{<<"yamerl">>,{pkg,<<"yamerl">>,<<"0.10.0">>},0},
|
{ref,"9675f9a25c2087e676660911987935caeff9fef8"}},
|
||||||
{<<"zj">>,
|
0}].
|
||||||
{git,"https://gitlab.com/zxq9/zj.git",
|
|
||||||
{ref,"090a43d23edc481695664f16763f147a78c45afc"}},
|
|
||||||
0}]}.
|
|
||||||
[
|
|
||||||
{pkg_hash,[
|
|
||||||
{<<"setup">>, <<"43C0BBFE9160DE7925BF2FC2FE4396A99DC4EE1B73F0DC46ACC3F10E27B07A9C">>},
|
|
||||||
{<<"yamerl">>, <<"4FF81FEE2F1F6A46F1700C0D880B24D193DDB74BD14EF42CB0BCF46E81EF2F8E">>}]},
|
|
||||||
{pkg_hash_ext,[
|
|
||||||
{<<"setup">>, <<"596713D48D8241DF31821C08A9F7BAAF3E7CDD042C8396BC956CC7AE056925DC">>},
|
|
||||||
{<<"yamerl">>, <<"346ADB2963F1051DC837A2364E4ACF6EB7D80097C0F53CBDC3046EC8EC4B4E6E">>}]}
|
|
||||||
].
|
|
||||||
|
|||||||
@@ -1,13 +1,13 @@
|
|||||||
%% -*- mode: erlang; erlang-indent-level: 4; indent-tabs-mode: nil -*-
|
%% -*- mode: erlang; erlang-indent-level: 4; indent-tabs-mode: nil -*-
|
||||||
{application, gmconfig,
|
{application, gmconfig,
|
||||||
[{description, "Gajumaru configuration management support"},
|
[{description, "Gajumaru configuration management support"},
|
||||||
{vsn, "0.1.0"},
|
{vsn, "zomp"},
|
||||||
{registered, []},
|
{registered, []},
|
||||||
{applications,
|
{applications,
|
||||||
[
|
[
|
||||||
kernel
|
kernel
|
||||||
, stdlib
|
, stdlib
|
||||||
, zj
|
, setup
|
||||||
]},
|
]},
|
||||||
{env, []},
|
{env, []},
|
||||||
{modules, []},
|
{modules, []},
|
||||||
|
|||||||
@@ -0,0 +1,14 @@
|
|||||||
|
%% -*- erlang-mode; erlang-indent-level: 4; indent-tabs-mode: nil -*-
|
||||||
|
|
||||||
|
[{application, Name, Opts}] = CONFIG.
|
||||||
|
case lists:keyfind(vsn, 1, Opts) of
|
||||||
|
{vsn, "zomp"} ->
|
||||||
|
ZompMetaF = filename:join(filename:dirname(filename:dirname(SCRIPT)), "zomp.meta"),
|
||||||
|
{ok, ZMeta} = file:consult(ZompMetaF),
|
||||||
|
{_, {_, _, {Vmaj,Vmin,Vpatch}}} = lists:keyfind(package_id, 1, ZMeta),
|
||||||
|
VsnStr = unicode:characters_to_list(io_lib:fwrite("~w.~w.~w", [Vmaj, Vmin, Vpatch])),
|
||||||
|
Opts1 = lists:keyreplace(vsn, 1, Opts, {vsn, VsnStr}),
|
||||||
|
[{application, Name, Opts1}];
|
||||||
|
_ ->
|
||||||
|
CONFIG
|
||||||
|
end.
|
||||||
+155
-69
@@ -7,6 +7,7 @@
|
|||||||
%%% @end
|
%%% @end
|
||||||
%%%-------------------------------------------------------------------
|
%%%-------------------------------------------------------------------
|
||||||
-module(gmconfig).
|
-module(gmconfig).
|
||||||
|
-vsn("0.2.0").
|
||||||
|
|
||||||
-export([get_env/2, get_env/3]).
|
-export([get_env/2, get_env/3]).
|
||||||
|
|
||||||
@@ -31,12 +32,14 @@
|
|||||||
-export([search_for_config_file/2]).
|
-export([search_for_config_file/2]).
|
||||||
-export([apply_os_env/0,
|
-export([apply_os_env/0,
|
||||||
apply_os_env/3]).
|
apply_os_env/3]).
|
||||||
|
-export([process_plain_args/0]).
|
||||||
-export([data_dir/1]).
|
-export([data_dir/1]).
|
||||||
-export([check_config/1]).
|
-export([check_config/1]).
|
||||||
|
|
||||||
-export([update_config/1,
|
-export([update_config/1,
|
||||||
update_config/2,
|
update_config/2,
|
||||||
update_config/3,
|
update_config/3,
|
||||||
|
pure_update_config/3,
|
||||||
silent_update_config/1,
|
silent_update_config/1,
|
||||||
delete_config_value/1,
|
delete_config_value/1,
|
||||||
suggest_config/2,
|
suggest_config/2,
|
||||||
@@ -48,6 +51,7 @@
|
|||||||
, gmconfig_env/1
|
, gmconfig_env/1
|
||||||
, gmconfig_env/2
|
, gmconfig_env/2
|
||||||
, set_gmconfig_env/1
|
, set_gmconfig_env/1
|
||||||
|
, default_gmconfig_env/0
|
||||||
]).
|
]).
|
||||||
|
|
||||||
-ifdef(TEST).
|
-ifdef(TEST).
|
||||||
@@ -62,11 +66,20 @@
|
|||||||
|
|
||||||
-include_lib("kernel/include/logger.hrl").
|
-include_lib("kernel/include/logger.hrl").
|
||||||
|
|
||||||
|
-type extension() :: string(). %% file extension (without leading dot)
|
||||||
|
|
||||||
|
-type decoder_return() :: config_tree()
|
||||||
|
| {ok, config_tree()}
|
||||||
|
| {error, any()}.
|
||||||
|
-type decoder_fun() :: fun( (binary()) -> decoder_return() ).
|
||||||
|
|
||||||
-type gmconfig() :: #{ os_env_prefix => string()
|
-type gmconfig() :: #{ os_env_prefix => string()
|
||||||
, config_file_basename => string() | 'undefined'
|
, config_file_basename => string() | 'undefined'
|
||||||
, config_file_os_env => string() | 'undefined'
|
, config_file_os_env => string() | 'undefined'
|
||||||
, config_file_search_path => [string() | fun(() -> string())]
|
, config_file_search_path => [string() | fun(() -> string())]
|
||||||
, system_suffix => string()
|
, config_plain_args => string() | 'undefined'
|
||||||
|
, system_suffix => string()
|
||||||
|
, config_formats => #{ extension() => decoder_fun() }
|
||||||
, schema => string() | map() | fun(() -> map())}.
|
, schema => string() | map() | fun(() -> map())}.
|
||||||
|
|
||||||
-type basic_type() :: number() | binary() | boolean().
|
-type basic_type() :: number() | binary() | boolean().
|
||||||
@@ -88,7 +101,7 @@ mock_config() ->
|
|||||||
|
|
||||||
mock_config(Cfg) ->
|
mock_config(Cfg) ->
|
||||||
ensure_schema_loaded(),
|
ensure_schema_loaded(),
|
||||||
store(Cfg, _Notify = false, _Mode = silent).
|
store(Cfg, _Mode = silent).
|
||||||
|
|
||||||
unmock_config() ->
|
unmock_config() ->
|
||||||
gmconfig_schema_utils:clear(),
|
gmconfig_schema_utils:clear(),
|
||||||
@@ -110,12 +123,13 @@ mock_system_defaults(Config) ->
|
|||||||
-endif.
|
-endif.
|
||||||
|
|
||||||
-spec set_gmconfig_env(gmconfig()) -> ok.
|
-spec set_gmconfig_env(gmconfig()) -> ok.
|
||||||
set_gmconfig_env(Env) when is_map(Env) ->
|
set_gmconfig_env(Env0) when is_map(Env0) ->
|
||||||
|
Env = maps:merge(default_gmconfig_env(), Env0),
|
||||||
persistent_term:put({?MODULE, gmconfig_env}, Env).
|
persistent_term:put({?MODULE, gmconfig_env}, Env).
|
||||||
|
|
||||||
-spec gmconfig_env() -> gmconfig().
|
-spec gmconfig_env() -> gmconfig().
|
||||||
gmconfig_env() ->
|
gmconfig_env() ->
|
||||||
persistent_term:get({?MODULE, gmconfig_env}, default_gmconfig()).
|
persistent_term:get({?MODULE, gmconfig_env}, default_gmconfig_env()).
|
||||||
|
|
||||||
-spec gmconfig_env(atom()) -> any().
|
-spec gmconfig_env(atom()) -> any().
|
||||||
gmconfig_env(Key) ->
|
gmconfig_env(Key) ->
|
||||||
@@ -142,11 +156,14 @@ search_path(Kind) ->
|
|||||||
end, Path0)
|
end, Path0)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
default_gmconfig() ->
|
default_gmconfig_env() ->
|
||||||
#{ os_env_prefix => "GM"
|
#{ os_env_prefix => "GM"
|
||||||
, config_file_basename => "gmconfig"
|
, config_file_basename => "gmconfig"
|
||||||
, config_file_os_env => undefined
|
, config_file_os_env => undefined
|
||||||
, config_file_search_path => ["."]
|
, config_file_search_path => ["."]
|
||||||
|
, config_formats => #{ "json" => fun json_decode/1
|
||||||
|
, "eterm" => fun eterm_consult/1 }
|
||||||
|
, config_plain_args => undefined
|
||||||
, system_defaults_search_path => [fun setup:data_dir/0]
|
, system_defaults_search_path => [fun setup:data_dir/0]
|
||||||
, system_suffix => "" }.
|
, system_suffix => "" }.
|
||||||
|
|
||||||
@@ -428,7 +445,20 @@ just_schema_keys(_) ->
|
|||||||
schema_default(Path) when is_list(Path) ->
|
schema_default(Path) when is_list(Path) ->
|
||||||
schema_default(Path, schema()).
|
schema_default(Path, schema()).
|
||||||
|
|
||||||
schema_default(Path, Schema) ->
|
schema_default(Path0, Schema) ->
|
||||||
|
%% There is a way to navigate through an array of objects
|
||||||
|
%% essentially locating the desired object in the array, and then
|
||||||
|
%% continuing into one of its properties. This is specified in
|
||||||
|
%% a path exression as `{PropName, Value, ThenProp}`.
|
||||||
|
%% If the query expects us to fall back to the schema default, then
|
||||||
|
%% we must adapt the path accordingly.
|
||||||
|
%%
|
||||||
|
%% find_config([<<"system">>, <<"plugins">>,
|
||||||
|
%% {<<"name">>, PluginName, <<"config">>} | Key],
|
||||||
|
%% [user_config, schema_default]);
|
||||||
|
Path = lists:flatmap(fun({_, _,P}) -> [<<"items">>, P];
|
||||||
|
(P) -> [P]
|
||||||
|
end, Path0),
|
||||||
case schema(Path, Schema) of
|
case schema(Path, Schema) of
|
||||||
undefined -> undefined;
|
undefined -> undefined;
|
||||||
{ok, Tree} ->
|
{ok, Tree} ->
|
||||||
@@ -496,7 +526,6 @@ load_config_file(File, Mode) when Mode =:= check;
|
|||||||
do_load_user_config(File, store, Mode).
|
do_load_user_config(File, store, Mode).
|
||||||
|
|
||||||
apply_os_env() ->
|
apply_os_env() ->
|
||||||
ok = application:ensure_started(gproc),
|
|
||||||
Pfx = os_env_prefix(),
|
Pfx = os_env_prefix(),
|
||||||
ConfigMap = pt_get_config(),
|
ConfigMap = pt_get_config(),
|
||||||
case apply_os_env(Pfx, schema(), ConfigMap) of
|
case apply_os_env(Pfx, schema(), ConfigMap) of
|
||||||
@@ -536,6 +565,35 @@ apply_os_env(Pfx, Schema, ConfigMap) ->
|
|||||||
error(E)
|
error(E)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
process_plain_args() ->
|
||||||
|
case gmconfig_env(config_plain_args, undefined) of
|
||||||
|
undefined ->
|
||||||
|
ok;
|
||||||
|
Str ->
|
||||||
|
PlainArgs = init:get_plain_arguments(),
|
||||||
|
Schema = schema(),
|
||||||
|
Map = process_plain_args_(PlainArgs, Str, schema(), #{}),
|
||||||
|
?LOG_INFO("Map from plain args: ~p", [Map]),
|
||||||
|
if map_size(Map) > 0 ->
|
||||||
|
update_config_(Map, pt_get_config(), Schema, report);
|
||||||
|
true ->
|
||||||
|
no_change
|
||||||
|
end
|
||||||
|
end.
|
||||||
|
|
||||||
|
process_plain_args_([Tag, K, V | Rest], Tag, Schema, Map) ->
|
||||||
|
Path = plain_path_arg(K),
|
||||||
|
Value = coerce_type(Path, V, Schema),
|
||||||
|
Map1 = update_map(to_map(Path, Value), Map),
|
||||||
|
process_plain_args_(Rest, Tag, Schema, Map1);
|
||||||
|
process_plain_args_([_ | T], Tag, Schema, Map) ->
|
||||||
|
process_plain_args_(T, Tag, Schema, Map);
|
||||||
|
process_plain_args_([], _, _, Map) ->
|
||||||
|
Map.
|
||||||
|
|
||||||
|
plain_path_arg(Str) ->
|
||||||
|
re:split(Str, <<"__">>, [{return, binary}]).
|
||||||
|
|
||||||
to_map(K, V) ->
|
to_map(K, V) ->
|
||||||
to_map(K, V, #{}).
|
to_map(K, V, #{}).
|
||||||
|
|
||||||
@@ -552,8 +610,8 @@ coerce_type(Key, Value, Schema) ->
|
|||||||
<<"integer">> -> to_integer(Value);
|
<<"integer">> -> to_integer(Value);
|
||||||
<<"string">> -> to_string(Value);
|
<<"string">> -> to_string(Value);
|
||||||
<<"boolean">> -> to_bool(Value);
|
<<"boolean">> -> to_bool(Value);
|
||||||
<<"array">> -> json_decode(list_to_binary(Value));
|
<<"array">> -> json:decode(list_to_binary(Value));
|
||||||
<<"object">> -> json_decode(list_to_binary(Value))
|
<<"object">> -> json:decode(list_to_binary(Value))
|
||||||
end;
|
end;
|
||||||
_ ->
|
_ ->
|
||||||
error({unknown_key, Key})
|
error({unknown_key, Key})
|
||||||
@@ -642,10 +700,21 @@ search_default_config() ->
|
|||||||
Basename ->
|
Basename ->
|
||||||
Dirs = search_path(),
|
Dirs = search_path(),
|
||||||
SystemSuffix = get_system_suffix(),
|
SystemSuffix = get_system_suffix(),
|
||||||
Fname = Basename ++ SystemSuffix ++ ".{json,yaml}",
|
ExtPattern = extension_pattern(),
|
||||||
|
Fname = Basename ++ SystemSuffix ++ ExtPattern,
|
||||||
search_for_config_file(Dirs, Fname)
|
search_for_config_file(Dirs, Fname)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
config_formats() ->
|
||||||
|
gmconfig_env(config_formats).
|
||||||
|
|
||||||
|
extension_pattern() ->
|
||||||
|
Exts = maps:keys(config_formats()),
|
||||||
|
lists:flatten([".{", intersperse(Exts),"}"]).
|
||||||
|
|
||||||
|
intersperse([H|T]) ->
|
||||||
|
[H | [[",", X] || X <- T]].
|
||||||
|
|
||||||
search_for_config_file(Dirs, FileWildcard) ->
|
search_for_config_file(Dirs, FileWildcard) ->
|
||||||
lists:foldl(
|
lists:foldl(
|
||||||
fun(D0, undefined) ->
|
fun(D0, undefined) ->
|
||||||
@@ -665,13 +734,16 @@ to_list_string(S) when is_list(S) ->
|
|||||||
binary_to_list(iolist_to_binary(S)).
|
binary_to_list(iolist_to_binary(S)).
|
||||||
|
|
||||||
do_load_user_config(F, Action, Mode) ->
|
do_load_user_config(F, Action, Mode) ->
|
||||||
case {filename:extension(F), Action} of
|
{Ext, Decoder} = pick_decoder(F),
|
||||||
{".json", store} -> store(read_json(F, Mode), Mode);
|
case Action of
|
||||||
{".yaml", store} -> store(read_yaml(F, Mode), Mode);
|
store -> store(read_file(F, Ext, Decoder, Mode), Mode);
|
||||||
{".json", check} -> check_config_(catch read_json(F, Mode));
|
check -> check_config_(catch read_file(F, Ext, Decoder, Mode))
|
||||||
{".yaml", check} -> check_config_(catch read_yaml(F, Mode))
|
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
pick_decoder(F) ->
|
||||||
|
"." ++ Ext = filename:extension(F),
|
||||||
|
{Ext, maps:get(Ext, config_formats())}.
|
||||||
|
|
||||||
store(Vars, Mode) when is_map(Vars) ->
|
store(Vars, Mode) when is_map(Vars) ->
|
||||||
case pt_get_config() of
|
case pt_get_config() of
|
||||||
Map when map_size(Map) == 0 ->
|
Map when map_size(Map) == 0 ->
|
||||||
@@ -726,12 +798,15 @@ silent_as_report(silent) -> report;
|
|||||||
silent_as_report(Mode ) -> Mode.
|
silent_as_report(Mode ) -> Mode.
|
||||||
|
|
||||||
schema_string(Schema, Mode) ->
|
schema_string(Schema, Mode) ->
|
||||||
JSONSchema = json:encode(Schema),
|
JSONSchema = json_encode(Schema),
|
||||||
case Mode of
|
case Mode of
|
||||||
check -> json:format(JSONSchema, #{indent => 2});
|
check -> json:format(JSONSchema, #{indent => 2});
|
||||||
report -> JSONSchema
|
report -> JSONSchema
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
json_encode(J) ->
|
||||||
|
iolist_to_binary(json:encode(J)).
|
||||||
|
|
||||||
error_format(Fmt, Args, check) ->
|
error_format(Fmt, Args, check) ->
|
||||||
io:format(Fmt, Args);
|
io:format(Fmt, Args);
|
||||||
error_format(Fmt, Args, report) ->
|
error_format(Fmt, Args, report) ->
|
||||||
@@ -818,6 +893,12 @@ update_config(Map, ConfigMap, Mode) ->
|
|||||||
pt_set_config(ConfigMap1),
|
pt_set_config(ConfigMap1),
|
||||||
ok.
|
ok.
|
||||||
|
|
||||||
|
pure_update_config(Map, ConfigMap, Schema) ->
|
||||||
|
NewCfg = gmconfig_schema_utils:merge(Map, ConfigMap, Schema),
|
||||||
|
check_validation([validate_(NewCfg, Schema)],
|
||||||
|
[NewCfg], pure_update_config, silent),
|
||||||
|
NewCfg.
|
||||||
|
|
||||||
export_config() ->
|
export_config() ->
|
||||||
Config = pt_get_config(),
|
Config = pt_get_config(),
|
||||||
JSON = json:format(Config, #{indent => 2}),
|
JSON = json:format(Config, #{indent => 2}),
|
||||||
@@ -906,70 +987,64 @@ update_map(With, Map) when is_map(With), is_map(Map) ->
|
|||||||
end
|
end
|
||||||
end, Map, With).
|
end, Map, With).
|
||||||
|
|
||||||
read_json(F, Mode) ->
|
read_file(F, Type, Decoder, Mode) ->
|
||||||
validate(
|
|
||||||
try_decode(F, fun(F1) ->
|
|
||||||
json_consult(F1)
|
|
||||||
end, "JSON", Mode), F, Mode).
|
|
||||||
|
|
||||||
json_consult(F) ->
|
|
||||||
case setup_file:read_file(F) of
|
case setup_file:read_file(F) of
|
||||||
{ok, Bin} ->
|
{ok, Bin} ->
|
||||||
[json_decode(Bin)];
|
validate(
|
||||||
|
try_decode_bin(Bin, Decoder, Type, Mode),
|
||||||
|
F, Mode);
|
||||||
{error, Reason} ->
|
{error, Reason} ->
|
||||||
?LOG_ERROR("Read error ~s - ~p", [F, Reason]),
|
error_msg(Mode, "Read error ~s - ~p", [F, Reason]),
|
||||||
error({read_error, F, Reason})
|
error({read_error, F, Reason})
|
||||||
end.
|
end.
|
||||||
|
|
||||||
json_decode(Bin) ->
|
json_decode(Str) when is_list(Str) ->
|
||||||
case json_decode_(Bin) of
|
json:decode(iolist_to_binary(Str));
|
||||||
{ok, Value} ->
|
json_decode(Bin) when is_binary(Bin) ->
|
||||||
Value;
|
json:decode(Bin).
|
||||||
{error, Reason} ->
|
|
||||||
?LOG_ERROR("CAUGHT: ~p", [Reason]),
|
eterm_consult(Bin) ->
|
||||||
error(Reason)
|
case setup_file:consult_binary(Bin) of
|
||||||
|
{ok, [Map]} when is_map(Map) ->
|
||||||
|
{ok, normalize_config(Map)};
|
||||||
|
{ok, Other} ->
|
||||||
|
error({unknown_data, Other});
|
||||||
|
{error, _} = Error ->
|
||||||
|
Error
|
||||||
end.
|
end.
|
||||||
|
|
||||||
json_decode_(Bin) ->
|
normalize_config(Map) when is_map(Map) ->
|
||||||
case zj:binary_decode(Bin) of
|
try
|
||||||
{ok, _} = Ok ->
|
json:decode(iolist_to_binary(json:encode(Map)))
|
||||||
Ok;
|
|
||||||
{error, _Parsed, Remainder} ->
|
|
||||||
{error, {json_decode_error, string_slice(Remainder)}}
|
|
||||||
end.
|
|
||||||
|
|
||||||
-define(SliceSz, 80).
|
|
||||||
string_slice(B) ->
|
|
||||||
case size(B) of
|
|
||||||
Sz when Sz > ?SliceSz ->
|
|
||||||
Sl = string:slice(B, 0, ?SliceSz - 4),
|
|
||||||
<<Sl/binary, " ...">>;
|
|
||||||
_ ->
|
|
||||||
B
|
|
||||||
end.
|
|
||||||
|
|
||||||
read_yaml(F, Mode) ->
|
|
||||||
validate(
|
|
||||||
try_decode(
|
|
||||||
F,
|
|
||||||
fun(F1) ->
|
|
||||||
yamerl:decode_file(F1, [{str_node_as_binary, true},
|
|
||||||
{map_node_format, map}])
|
|
||||||
end, "YAML", Mode),
|
|
||||||
F, Mode).
|
|
||||||
|
|
||||||
try_decode(F, DecF, Fmt, Mode) ->
|
|
||||||
try DecF(F)
|
|
||||||
catch
|
catch
|
||||||
error:E ->
|
error:E ->
|
||||||
error_msg(Mode, "Error reading ~s file: ~s~n", [Fmt, F]),
|
error({cannot_normalize, E, Map})
|
||||||
erlang:error(E)
|
|
||||||
end.
|
end.
|
||||||
|
|
||||||
validate(JSON, F, Mode) when is_list(JSON) ->
|
try_decode_bin(Bin, Decoder, Fmt, Mode) ->
|
||||||
check_validation([validate_(J) || J <- JSON], JSON, F, Mode).
|
try decode_bin(Bin, Decoder)
|
||||||
%% validate(JSON, F, Mode) when is_map(JSON) ->
|
catch
|
||||||
%% validate([JSON], F, Mode).
|
error:E:T ->
|
||||||
|
error_msg(Mode, "CAUGHT for ~p: ~p / ~p", [Fmt, E, T]),
|
||||||
|
error(E)
|
||||||
|
end.
|
||||||
|
|
||||||
|
decode_bin(Str, Decoder) when is_list(Str) ->
|
||||||
|
decode_bin(iolist_to_binary(Str), Decoder);
|
||||||
|
decode_bin(Bin, Decoder) when is_binary(Bin), is_function(Decoder, 1) ->
|
||||||
|
case Decoder(Bin) of
|
||||||
|
{ok, Map} when is_map(Map) ->
|
||||||
|
Map;
|
||||||
|
Map when is_map(Map) ->
|
||||||
|
Map;
|
||||||
|
{error, E} ->
|
||||||
|
error(E);
|
||||||
|
Other ->
|
||||||
|
error({bad_decoder_return, Other})
|
||||||
|
end.
|
||||||
|
|
||||||
|
validate(JSON, F, Mode) when is_map(JSON) ->
|
||||||
|
check_validation([validate_(JSON)], JSON, F, Mode).
|
||||||
|
|
||||||
vinfo(Mode, Res, F) ->
|
vinfo(Mode, Res, F) ->
|
||||||
case Res of
|
case Res of
|
||||||
@@ -1071,7 +1146,8 @@ ensure_schema_loaded() ->
|
|||||||
|
|
||||||
load_schema() ->
|
load_schema() ->
|
||||||
JSON = gmconfig_schema(),
|
JSON = gmconfig_schema(),
|
||||||
Schema = json_decode(JSON),
|
Decode = maps:get("json", config_formats()),
|
||||||
|
Schema = decode_bin(JSON, Decode),
|
||||||
pt_set_schema(Schema),
|
pt_set_schema(Schema),
|
||||||
Schema.
|
Schema.
|
||||||
|
|
||||||
@@ -1084,3 +1160,13 @@ gmconfig_schema() ->
|
|||||||
F when is_function(F, 0) ->
|
F when is_function(F, 0) ->
|
||||||
F()
|
F()
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
json_consult(F) ->
|
||||||
|
Decode = maps:get("json", config_formats()),
|
||||||
|
case setup_file:read_file(F) of
|
||||||
|
{ok, Bin} ->
|
||||||
|
decode_bin(Bin, Decode);
|
||||||
|
{error, Reason} ->
|
||||||
|
?LOG_ERROR("Read error ~s - ~p", [F, Reason]),
|
||||||
|
error({read_error, F, Reason})
|
||||||
|
end.
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
-module(gmconfig_schema_helpers).
|
-module(gmconfig_schema_helpers).
|
||||||
|
-vsn("0.2.0").
|
||||||
|
|
||||||
-export(
|
-export(
|
||||||
[
|
[
|
||||||
|
|||||||
+200
-50
@@ -1,5 +1,6 @@
|
|||||||
%% -*- mode: erlang; erlang-indent-level: 4; indent-tabs-mode: nil -*-
|
%% -*- mode: erlang; erlang-indent-level: 4; indent-tabs-mode: nil -*-
|
||||||
-module(gmconfig_schema_utils).
|
-module(gmconfig_schema_utils).
|
||||||
|
-vsn("0.2.0").
|
||||||
|
|
||||||
|
|
||||||
-export([get_config/0,
|
-export([get_config/0,
|
||||||
@@ -7,20 +8,28 @@
|
|||||||
get_schema/0,
|
get_schema/0,
|
||||||
get_schema/1, %% (Default)
|
get_schema/1, %% (Default)
|
||||||
set_schema/1,
|
set_schema/1,
|
||||||
|
use_schema/1,
|
||||||
|
use_schema/2,
|
||||||
schema/1, %% (Path)
|
schema/1, %% (Path)
|
||||||
schema/2, %% (Path, Schema)
|
schema/2, %% (Path, Schema)
|
||||||
schema/3, %% (Path, Schema, Opts)
|
schema/3, %% (Path, Schema, Opts)
|
||||||
clear/0,
|
clear/0,
|
||||||
expand_schema/1]).
|
expand_ref/2,
|
||||||
|
expand_schema/1, %% (Schema) %% expand whole schema
|
||||||
|
expand_schema/2]). %% (SubSchema, RootSchema)
|
||||||
|
|
||||||
-export([ update_config/1 %% (Map) -> ok
|
-export([ update_config/1 %% (Map) -> ok
|
||||||
, merge/2 %% (Item1, Item2) -> Item3
|
, merge/2 %% (Item1, Item2) -> Item3
|
||||||
, merge/3 %% (Item1, Item2, Schema) -> Item3
|
, merge/3 %% (Item1, Item2, Schema) -> Item3
|
||||||
, valid/1 %% (Item) -> Item | error()
|
, valid/1 %% (Item) -> Item | error()
|
||||||
, valid/2 %% (Item, Schema) -> Item | error()
|
, valid/2 %% (Item, Schema) -> Item | error()
|
||||||
|
, validate/3 %% (Item, Schema, Opts) -> Item | error().
|
||||||
]).
|
]).
|
||||||
-export([in_properties/2]).
|
-export([in_properties/2]).
|
||||||
|
|
||||||
|
-export([normalize/0,
|
||||||
|
normalize/1]).
|
||||||
|
|
||||||
-type json_string() :: binary().
|
-type json_string() :: binary().
|
||||||
-type json_int() :: integer().
|
-type json_int() :: integer().
|
||||||
-type json_num() :: number().
|
-type json_num() :: number().
|
||||||
@@ -37,15 +46,26 @@
|
|||||||
|
|
||||||
-type schema() :: json().
|
-type schema() :: json().
|
||||||
|
|
||||||
|
-type ext_fun() :: fun( (json(), schema()) -> any() | no_return() ).
|
||||||
|
-type extensions() :: #{ binary() => ext_fun() }.
|
||||||
|
-type options() :: #{coerce => boolean(),
|
||||||
|
enum_to_atom => boolean(),
|
||||||
|
extensions => extensions() }.
|
||||||
|
|
||||||
-record(st, { s :: schema() %% schema
|
-record(st, { s :: schema() %% schema
|
||||||
, r :: schema() %% root schema
|
, r :: schema() %% root schema
|
||||||
, p = []
|
, p = []
|
||||||
, a = [] %% annotations
|
, a = [] %% annotations
|
||||||
, v :: json() | undefined %% value
|
, v :: json() | undefined %% value
|
||||||
|
, d = undefined :: list() | 'undefined' %% dynamic eval
|
||||||
|
, opts = #{} :: options()
|
||||||
}).
|
}).
|
||||||
|
|
||||||
-type st() :: #st{}.
|
-type st() :: #st{}.
|
||||||
|
|
||||||
|
-export_type([ schema/0, json/0 ]).
|
||||||
|
|
||||||
|
-include_lib("kernel/include/logger.hrl").
|
||||||
|
|
||||||
-spec set_schema(schema()) -> ok.
|
-spec set_schema(schema()) -> ok.
|
||||||
set_schema(Schema) ->
|
set_schema(Schema) ->
|
||||||
@@ -67,6 +87,26 @@ get_schema() ->
|
|||||||
get_schema(Default) ->
|
get_schema(Default) ->
|
||||||
persistent_term:get({?MODULE, '$schema'}, Default).
|
persistent_term:get({?MODULE, '$schema'}, Default).
|
||||||
|
|
||||||
|
-spec use_schema(schema() | st()) -> st().
|
||||||
|
use_schema(#st{} = St) -> St;
|
||||||
|
use_schema(S) -> #st{s = S, r = S}.
|
||||||
|
|
||||||
|
use_schema(Schema, RootSchema) ->
|
||||||
|
#st{s = Schema, r = RootSchema}.
|
||||||
|
|
||||||
|
normalize() ->
|
||||||
|
normalize(get_schema()).
|
||||||
|
|
||||||
|
normalize(S) when is_map(S) ->
|
||||||
|
#{bin_key(K) => normalize(V) || K := V <- S};
|
||||||
|
normalize(S) when is_list(S) ->
|
||||||
|
[normalize(Sx) || Sx <- S];
|
||||||
|
normalize(S) ->
|
||||||
|
S.
|
||||||
|
|
||||||
|
bin_key(A) when is_atom(A) -> atom_to_binary(A, utf8);
|
||||||
|
bin_key(B) when is_binary(B) -> B.
|
||||||
|
|
||||||
clear() ->
|
clear() ->
|
||||||
persistent_term:erase({?MODULE,'$schema'}),
|
persistent_term:erase({?MODULE,'$schema'}),
|
||||||
persistent_term:erase({?MODULE,'$config'}),
|
persistent_term:erase({?MODULE,'$config'}),
|
||||||
@@ -138,12 +178,12 @@ schema_get(_, _, Default) ->
|
|||||||
|
|
||||||
%% let us pattern-match on a schema map
|
%% let us pattern-match on a schema map
|
||||||
%% all schemas that are not a map are converted to the empty map.
|
%% all schemas that are not a map are converted to the empty map.
|
||||||
schema_map(Map) when is_map(Map) -> Map;
|
schema_map(Map, _) when is_map(Map) -> Map;
|
||||||
schema_map(_) -> #{}.
|
schema_map(_, _) -> #{}.
|
||||||
|
|
||||||
-spec merge_(json(), json(), st()) -> json().
|
-spec merge_(json(), json(), st()) -> json().
|
||||||
merge_(A, B, #st{} = St) ->
|
merge_(A, B, #st{} = St0) ->
|
||||||
Ss = schemas_from_dynamic_eval(A, St),
|
{Ss, St} = schemas_from_dynamic_eval(A, St0#st{d = undefined}),
|
||||||
case schema_prop(<<"readOnly">>, St, Ss, false) of
|
case schema_prop(<<"readOnly">>, St, Ss, false) of
|
||||||
true when B == null ->
|
true when B == null ->
|
||||||
valid(A, St);
|
valid(A, St);
|
||||||
@@ -163,7 +203,7 @@ merge_(A, B, St, Ss) ->
|
|||||||
end.
|
end.
|
||||||
|
|
||||||
update_semantics(A, St, Ss) ->
|
update_semantics(A, St, Ss) ->
|
||||||
case maps:find(<<"$updateSemantics">>, schema_map(A)) of
|
case maps:find(<<"$updateSemantics">>, schema_map(A, St)) of
|
||||||
{ok, _} = Ok ->
|
{ok, _} = Ok ->
|
||||||
{Ok, object};
|
{Ok, object};
|
||||||
error ->
|
error ->
|
||||||
@@ -190,9 +230,9 @@ remove_props(O, Keys, Recurse) when is_map(O) ->
|
|||||||
remove_props(Other, _, _) ->
|
remove_props(Other, _, _) ->
|
||||||
Other.
|
Other.
|
||||||
|
|
||||||
get_type(#st{} = St, Value) ->
|
get_type(#st{} = St0, Value) ->
|
||||||
Ss = schemas_from_dynamic_eval(Value, St),
|
{Ss, St} = schemas_from_dynamic_eval(Value, St0),
|
||||||
get_type(St, Ss, Value).
|
{get_type(St, Ss, Value), St}.
|
||||||
|
|
||||||
get_type(#st{} = St, Ss, Value) ->
|
get_type(#st{} = St, Ss, Value) ->
|
||||||
case any_schema_prop(<<"type">>, St, Ss) of
|
case any_schema_prop(<<"type">>, St, Ss) of
|
||||||
@@ -272,7 +312,7 @@ update_object(A0, B, St, Ss) ->
|
|||||||
|
|
||||||
update_object_(New, Old, St, Ss) ->
|
update_object_(New, Old, St, Ss) ->
|
||||||
Dyn = acc_props(Ss),
|
Dyn = acc_props(Ss),
|
||||||
SsOld = schemas_from_dynamic_eval(Old, St),
|
{SsOld, _} = schemas_from_dynamic_eval(Old, St#st{d = undefined}),
|
||||||
PropSchemas = [{P, prop_schema(P, Dyn, St)} || P <- maps:keys(New)],
|
PropSchemas = [{P, prop_schema(P, Dyn, St)} || P <- maps:keys(New)],
|
||||||
try do_update_object(New, Old, St, PropSchemas)
|
try do_update_object(New, Old, St, PropSchemas)
|
||||||
catch
|
catch
|
||||||
@@ -300,6 +340,55 @@ do_update_object(New, Old, St, PropSchemas) ->
|
|||||||
end, Old, PropSchemas),
|
end, Old, PropSchemas),
|
||||||
valid(Res, object, St).
|
valid(Res, object, St).
|
||||||
|
|
||||||
|
validate(V, Schema, Opts) when is_map(Opts) ->
|
||||||
|
St0 = use_schema(Schema),
|
||||||
|
St = St0#st{opts = Opts},
|
||||||
|
V1 = valid(V, St),
|
||||||
|
case Opts of
|
||||||
|
#{enum_to_atom := true} ->
|
||||||
|
convert_enums(V1, St);
|
||||||
|
_ ->
|
||||||
|
V1
|
||||||
|
end.
|
||||||
|
|
||||||
|
convert_enums(V, St0) when is_binary(V) ->
|
||||||
|
case get_type(St0, V) of
|
||||||
|
{string, St} ->
|
||||||
|
{Ss, St1} = schemas_from_dynamic_eval(V, St),
|
||||||
|
case any_schema_prop(<<"enum">>, St1, Ss) of
|
||||||
|
{ok, _} ->
|
||||||
|
binary_to_atom(V, utf8);
|
||||||
|
_ ->
|
||||||
|
V
|
||||||
|
end;
|
||||||
|
_ ->
|
||||||
|
V
|
||||||
|
end;
|
||||||
|
convert_enums(V, St0) when is_map(V) ->
|
||||||
|
{Ss, St} = schemas_from_dynamic_eval(V, St0),
|
||||||
|
Dyn = acc_props(Ss),
|
||||||
|
maps:map(
|
||||||
|
fun(P, Vp) ->
|
||||||
|
PSchema = prop_schema(P, Dyn, St),
|
||||||
|
convert_enums(Vp, push_path(P, s(PSchema, St)))
|
||||||
|
end, V);
|
||||||
|
convert_enums(V, St0) when is_list(V) ->
|
||||||
|
{Ss,St} = schemas_from_dynamic_eval(V, St0),
|
||||||
|
case any_schema_prop(<<"items">>, St, Ss) of
|
||||||
|
{ok, Is} ->
|
||||||
|
[convert_enums(Vi, push_path(items, s(Is, St)))
|
||||||
|
|| Vi <- V];
|
||||||
|
error ->
|
||||||
|
case any_schema_prop(<<"prefixItems">>, St, Ss) of
|
||||||
|
{ok, PfxIs} ->
|
||||||
|
[convert_enums(Vi, push_path(prefixItems, s(PfxIs, St)))
|
||||||
|
|| Vi <- V];
|
||||||
|
error ->
|
||||||
|
V
|
||||||
|
end
|
||||||
|
end;
|
||||||
|
convert_enums(V, _) ->
|
||||||
|
V.
|
||||||
|
|
||||||
valid(V) ->
|
valid(V) ->
|
||||||
valid(V, get_schema()).
|
valid(V, get_schema()).
|
||||||
@@ -311,21 +400,21 @@ valid(V, Schema) ->
|
|||||||
|
|
||||||
valid_(V, #st{s = true}) -> V;
|
valid_(V, #st{s = true}) -> V;
|
||||||
valid_(_, #st{s = false} = St) -> fail(invalid, St);
|
valid_(_, #st{s = false} = St) -> fail(invalid, St);
|
||||||
valid_(V, St) ->
|
valid_(V, St0) ->
|
||||||
Type = get_type(St, V),
|
{Type, St} = get_type(St0, V),
|
||||||
valid(V, Type, St).
|
valid(V, Type, St).
|
||||||
|
|
||||||
valid(V, _, #st{s = true}) -> V;
|
valid(V, _, #st{s = true}) -> V;
|
||||||
valid(_, _, #st{s = false} = St) -> fail(invalid, St);
|
valid(_, _, #st{s = false} = St) -> fail(invalid, St);
|
||||||
valid(V, Type, St) ->
|
valid(V, Type, St0) ->
|
||||||
%% We run dynamic eval to find conditional parts of the schema.
|
%% We run dynamic eval to find conditional parts of the schema.
|
||||||
%% we keep these in a separate list.
|
%% we keep these in a separate list.
|
||||||
Ss = schemas_from_dynamic_eval(V, St),
|
{Ss,St} = schemas_from_dynamic_eval(V, St0),
|
||||||
valid(V, Type, St, Ss).
|
valid(V, Type, St, Ss).
|
||||||
|
|
||||||
valid(V, Type, St, Ss) ->
|
valid(V, Type, St, Ss) ->
|
||||||
valid_const(V, Type, St, Ss),
|
_ = valid_const(V, Type, St, Ss),
|
||||||
valid_enum(V, Type, St, Ss),
|
_ = valid_enum(V, Type, St, Ss),
|
||||||
%% Dynamic eval returns a list of matching schemas
|
%% Dynamic eval returns a list of matching schemas
|
||||||
%% We pass them along as they may contain properties,
|
%% We pass them along as they may contain properties,
|
||||||
%% but `V` has already been validated against them.
|
%% but `V` has already been validated against them.
|
||||||
@@ -342,7 +431,7 @@ valid(V, Type, St, Ss) ->
|
|||||||
split_valid(V, St, Ss) ->
|
split_valid(V, St, Ss) ->
|
||||||
split_valid(V, 0, St, Ss, [], []).
|
split_valid(V, 0, St, Ss, [], []).
|
||||||
split_valid(V, Ix, St, [S|Ss], Yes, No) ->
|
split_valid(V, Ix, St, [S|Ss], Yes, No) ->
|
||||||
try valid(V, push_path(Ix, St#st{s = S})) of
|
try valid(V, push_path(Ix, s(S, St))) of
|
||||||
_ -> split_valid(V, Ix+1, St, Ss, [{Ix,S}|Yes], No)
|
_ -> split_valid(V, Ix+1, St, Ss, [{Ix,S}|Yes], No)
|
||||||
catch
|
catch
|
||||||
error:Err ->
|
error:Err ->
|
||||||
@@ -370,7 +459,8 @@ valid_enum(V, Type, St, Ss) ->
|
|||||||
case lists:any(fun(X) ->
|
case lists:any(fun(X) ->
|
||||||
is_equal(Type, V, X)
|
is_equal(Type, V, X)
|
||||||
end, En) of
|
end, En) of
|
||||||
true -> V;
|
true ->
|
||||||
|
V;
|
||||||
false ->
|
false ->
|
||||||
fail(not_in_enum, push_path(enum, St))
|
fail(not_in_enum, push_path(enum, St))
|
||||||
end
|
end
|
||||||
@@ -396,7 +486,7 @@ valid_object(O, St, Ss) when is_map(O) ->
|
|||||||
end,
|
end,
|
||||||
lists:foreach(
|
lists:foreach(
|
||||||
fun({P, #st{} = S}) ->
|
fun({P, #st{} = S}) ->
|
||||||
valid(maps:get(P, O), push_path(P, S))
|
valid(maps:get(P, O), push_path(P, S#st{d = undefined}))
|
||||||
end, PropSchemas),
|
end, PropSchemas),
|
||||||
O;
|
O;
|
||||||
valid_object(_, St, _) ->
|
valid_object(_, St, _) ->
|
||||||
@@ -405,12 +495,15 @@ valid_object(_, St, _) ->
|
|||||||
-spec valid_boolean(json(), st(), [st()]) -> json_bool().
|
-spec valid_boolean(json(), st(), [st()]) -> json_bool().
|
||||||
valid_boolean(V, #st{s = true}, []) -> V;
|
valid_boolean(V, #st{s = true}, []) -> V;
|
||||||
valid_boolean(_, #st{s = false} = St, []) -> fail(invalid, St);
|
valid_boolean(_, #st{s = false} = St, []) -> fail(invalid, St);
|
||||||
|
valid_boolean(<<"true">> , #st{opts = #{coerce := true}}, _) -> true;
|
||||||
|
valid_boolean(<<"false">>, #st{opts = #{coerce := true}}, _) -> false;
|
||||||
valid_boolean(V, St, _) ->
|
valid_boolean(V, St, _) ->
|
||||||
assert_type(fun is_boolean/1, V, St),
|
assert_type(fun is_boolean/1, V, St),
|
||||||
V.
|
V.
|
||||||
|
|
||||||
valid_null(N, #st{s = true}, []) -> N;
|
valid_null(N, #st{s = true}, []) -> N;
|
||||||
valid_null(_, #st{s = false} = St, []) -> fail(invalid, St);
|
valid_null(_, #st{s = false} = St, []) -> fail(invalid, St);
|
||||||
|
valid_null(<<"null">>, #st{s = null, opts = #{coerce := true}}, _) -> null;
|
||||||
valid_null(null, #st{s = null}, _) ->
|
valid_null(null, #st{s = null}, _) ->
|
||||||
null;
|
null;
|
||||||
valid_null(_, St, _) ->
|
valid_null(_, St, _) ->
|
||||||
@@ -432,15 +525,28 @@ valid_string(S, St, Ss) when is_binary(S) ->
|
|||||||
Lmax = schema_prop(<<"maxLength">>, St, Ss, Sz),
|
Lmax = schema_prop(<<"maxLength">>, St, Ss, Sz),
|
||||||
assert_min(Sz, Lmin, min_length, St),
|
assert_min(Sz, Lmin, min_length, St),
|
||||||
assert_max(Sz, Lmax, max_length, St),
|
assert_max(Sz, Lmax, max_length, St),
|
||||||
S;
|
valid_enum(S, string, St, Ss);
|
||||||
valid_string(_, St, _) ->
|
valid_string(_, St, _) ->
|
||||||
fail(wrong_type, St).
|
fail(wrong_type, St).
|
||||||
|
|
||||||
|
|
||||||
valid_number(N, _, #st{s = true}, []) -> N;
|
valid_number(N, _, #st{s = true}, []) -> N;
|
||||||
valid_number(_, _, #st{s = false} = St, []) -> fail(invalid, St);
|
valid_number(_, _, #st{s = false} = St, []) -> fail(invalid, St);
|
||||||
|
valid_number(I, Sub, #st{opts = #{coerce := true}} = St, Ss) when is_binary(I) ->
|
||||||
|
try coerce_num(Sub, I) of
|
||||||
|
I1 ->
|
||||||
|
valid_number_(I1, Sub, St#st{v = I1}, Ss)
|
||||||
|
catch
|
||||||
|
error:_ ->
|
||||||
|
fail(wrong_type, St)
|
||||||
|
end;
|
||||||
valid_number(I, Sub, St, Ss) when is_number(I) ->
|
valid_number(I, Sub, St, Ss) when is_number(I) ->
|
||||||
[assert_type(fun is_integer/1, I, St) || Sub == integer],
|
valid_number_(I, Sub, St, Ss);
|
||||||
|
valid_number(_, _, St, _) ->
|
||||||
|
fail(wrong_type, St).
|
||||||
|
|
||||||
|
valid_number_(I, Sub, St, Ss) when is_number(I) ->
|
||||||
|
[assert_type(fun is_integer/1, I, St) || Sub == integer],
|
||||||
case any_schema_prop(<<"multipleOf">>, St, Ss) of
|
case any_schema_prop(<<"multipleOf">>, St, Ss) of
|
||||||
error -> ok;
|
error -> ok;
|
||||||
{ok, X} when is_integer(X), X > 0 ->
|
{ok, X} when is_integer(X), X > 0 ->
|
||||||
@@ -473,9 +579,16 @@ valid_number(I, Sub, St, Ss) when is_number(I) ->
|
|||||||
EMax = schema_prop(<<"exclusiveMaximum">>, St, Ss, I+1),
|
EMax = schema_prop(<<"exclusiveMaximum">>, St, Ss, I+1),
|
||||||
test_range('>', EMax, I, add_anno(EMax, push_path(exclusiveMaximum, St))),
|
test_range('>', EMax, I, add_anno(EMax, push_path(exclusiveMaximum, St))),
|
||||||
test_range('<', EMin, I, add_anno(EMin, push_path(exclusiveMinimum, St))),
|
test_range('<', EMin, I, add_anno(EMin, push_path(exclusiveMinimum, St))),
|
||||||
I;
|
I.
|
||||||
valid_number(_, _, St, _) ->
|
|
||||||
fail(wrong_type, St).
|
coerce_num(integer, I) when is_binary(I) ->
|
||||||
|
binary_to_integer(I);
|
||||||
|
coerce_num(number, I) when is_binary(I) ->
|
||||||
|
try binary_to_integer(I)
|
||||||
|
catch
|
||||||
|
error:_ ->
|
||||||
|
binary_to_float(I)
|
||||||
|
end.
|
||||||
|
|
||||||
valid_array(A, #st{s = true}, []) -> A;
|
valid_array(A, #st{s = true}, []) -> A;
|
||||||
valid_array(_, #st{s = false} = St, []) -> fail(invalid, St);
|
valid_array(_, #st{s = false} = St, []) -> fail(invalid, St);
|
||||||
@@ -497,23 +610,23 @@ valid_array(A, #st{} = St, Ss) when is_list(A) ->
|
|||||||
{ok, PfxIs} ->
|
{ok, PfxIs} ->
|
||||||
assert_schema(fun is_list/1, PfxIs, push_path(prefixItems, St)),
|
assert_schema(fun is_list/1, PfxIs, push_path(prefixItems, St)),
|
||||||
check_prefix_items(
|
check_prefix_items(
|
||||||
PfxIs, A, Is, push_path(prefixItems, St#st{s = PfxIs}));
|
PfxIs, A, Is, push_path(prefixItems, s(PfxIs, St)));
|
||||||
error ->
|
error ->
|
||||||
check_items(A, push_path(items, St#st{s = Is}))
|
check_items(A, push_path(items, s(Is, St)))
|
||||||
end;
|
end;
|
||||||
error ->
|
error ->
|
||||||
case PfxItems of
|
case PfxItems of
|
||||||
{ok, PfxIs} ->
|
{ok, PfxIs} ->
|
||||||
assert_schema(fun is_list/1, PfxIs, push_path(prefixItems, St)),
|
assert_schema(fun is_list/1, PfxIs, push_path(prefixItems, St)),
|
||||||
check_prefix_items(
|
check_prefix_items(
|
||||||
PfxIs, A, true, push_path(prefixItems, St#st{s = PfxIs}));
|
PfxIs, A, true, push_path(prefixItems, s(PfxIs, St)));
|
||||||
error ->
|
error ->
|
||||||
ok
|
ok
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
case any_schema_prop(<<"contains">>, St, Ss) of
|
case any_schema_prop(<<"contains">>, St, Ss) of
|
||||||
{ok, Cs} ->
|
{ok, Cs} ->
|
||||||
check_contains(A, push_path(contains, St#st{s = Cs}),
|
check_contains(A, push_path(contains, s(Cs, St)),
|
||||||
schema_prop(<<"minContains">>, St, Ss, null),
|
schema_prop(<<"minContains">>, St, Ss, null),
|
||||||
schema_prop(<<"maxContains">>, St, Ss, null));
|
schema_prop(<<"maxContains">>, St, Ss, null));
|
||||||
error ->
|
error ->
|
||||||
@@ -540,12 +653,12 @@ check_prefix_items(_, _, _, St) ->
|
|||||||
fail(invalid, St).
|
fail(invalid, St).
|
||||||
|
|
||||||
check_prefix_items([I|Is], [H|T], Ix, Items, St) ->
|
check_prefix_items([I|Is], [H|T], Ix, Items, St) ->
|
||||||
_ = valid(H, push_path(Ix, St#st{s = I})),
|
_ = valid(H, push_path(Ix, s(I, St))),
|
||||||
check_prefix_items(Is, T, Ix+1, Items, St);
|
check_prefix_items(Is, T, Ix+1, Items, St);
|
||||||
check_prefix_items(_, [], _, _, _) ->
|
check_prefix_items(_, [], _, _, _) ->
|
||||||
ok;
|
ok;
|
||||||
check_prefix_items([], Rest, Ix, Items, St) ->
|
check_prefix_items([], Rest, Ix, Items, St) ->
|
||||||
check_items(Rest, Ix, push_path(items, St#st{s = Items})).
|
check_items(Rest, Ix, push_path(items, s(Items, St))).
|
||||||
|
|
||||||
check_items(A, St) ->
|
check_items(A, St) ->
|
||||||
check_items(A, 0, St).
|
check_items(A, 0, St).
|
||||||
@@ -577,13 +690,13 @@ check_contains([], _, St, Min, Max, Yes, _No) ->
|
|||||||
if is_integer(Max) ->
|
if is_integer(Max) ->
|
||||||
_ = valid(YesLen,
|
_ = valid(YesLen,
|
||||||
push_path(max,
|
push_path(max,
|
||||||
St#st{s = #{<<"maximum">> => Max}}));
|
s(#{<<"maximum">> => Max}, St)));
|
||||||
true -> ok
|
true -> ok
|
||||||
end,
|
end,
|
||||||
if is_integer(Min) ->
|
if is_integer(Min) ->
|
||||||
_ = valid(YesLen,
|
_ = valid(YesLen,
|
||||||
push_path(min,
|
push_path(min,
|
||||||
St#st{s = #{<<"minimum">> => Min}}));
|
s(#{<<"minimum">> => Min}, St)));
|
||||||
true ->
|
true ->
|
||||||
ok
|
ok
|
||||||
end
|
end
|
||||||
@@ -660,14 +773,26 @@ any_pattern_({Pat, Schema, I}, P) ->
|
|||||||
any_pattern_(maps:next(I), P)
|
any_pattern_(maps:next(I), P)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
schemas_from_dynamic_eval(Obj, #st{s = Schema} = St) ->
|
maybe_expand_ref(#st{s = S} = St) ->
|
||||||
SMap = schema_map(Schema),
|
case S of
|
||||||
|
#{<<"$ref">> := Ref} = R when map_size(R) == 1 ->
|
||||||
|
St#st{s = expand_ref(Ref, St#st.r)};
|
||||||
|
_ ->
|
||||||
|
St
|
||||||
|
end.
|
||||||
|
|
||||||
|
schemas_from_dynamic_eval(_, #st{d = Ss} = St) when Ss =/= undefined ->
|
||||||
|
{Ss, St};
|
||||||
|
schemas_from_dynamic_eval(Obj, #st{s = Schema} = St0) ->
|
||||||
|
St = maybe_expand_ref(St0),
|
||||||
|
SMap = schema_map(Schema, St),
|
||||||
|
Ss =
|
||||||
maps:fold(
|
maps:fold(
|
||||||
fun(<<"allOf">>, Ss, Acc) ->
|
fun(<<"allOf">>, Ss, Acc) ->
|
||||||
St1 = push_path(allOf, St),
|
St1 = push_path(allOf, St),
|
||||||
case split_valid(Obj, St, Ss) of
|
case split_valid(Obj, St, Ss) of
|
||||||
{ValidSs, []} ->
|
{ValidSs, []} ->
|
||||||
Acc ++ [St1#st{s = S} || {_, S} <- ValidSs];
|
Acc ++ [s(S, St1) || {_, S} <- ValidSs];
|
||||||
{_, FailedSs} ->
|
{_, FailedSs} ->
|
||||||
fail(failing_schemas, add_anno(FailedSs, St1))
|
fail(failing_schemas, add_anno(FailedSs, St1))
|
||||||
end;
|
end;
|
||||||
@@ -675,7 +800,7 @@ schemas_from_dynamic_eval(Obj, #st{s = Schema} = St) ->
|
|||||||
St1 = push_path(anyOf, St),
|
St1 = push_path(anyOf, St),
|
||||||
case split_valid(Obj, St1, Ss) of
|
case split_valid(Obj, St1, Ss) of
|
||||||
{[_|_] = ValidSs, _} ->
|
{[_|_] = ValidSs, _} ->
|
||||||
Acc ++ [St1#st{s = S} || {_, S} <- ValidSs];
|
Acc ++ [s(S, St1) || {_, S} <- ValidSs];
|
||||||
{[], FailedSs} ->
|
{[], FailedSs} ->
|
||||||
fail(no_matching_schema, add_anno(FailedSs, St1))
|
fail(no_matching_schema, add_anno(FailedSs, St1))
|
||||||
end;
|
end;
|
||||||
@@ -683,7 +808,7 @@ schemas_from_dynamic_eval(Obj, #st{s = Schema} = St) ->
|
|||||||
St1 = push_path(oneOf, St),
|
St1 = push_path(oneOf, St),
|
||||||
case split_valid(Obj, St1, Ss) of
|
case split_valid(Obj, St1, Ss) of
|
||||||
{[{_, S}], _} ->
|
{[{_, S}], _} ->
|
||||||
Acc ++ [St1#st{s = S}];
|
Acc ++ [s(S, St1)];
|
||||||
{[_|_] = MoreValid, _} ->
|
{[_|_] = MoreValid, _} ->
|
||||||
ValidIxs = [I || {I,_} <- MoreValid],
|
ValidIxs = [I || {I,_} <- MoreValid],
|
||||||
fail(more_than_one, add_anno({valid, ValidIxs}, St1));
|
fail(more_than_one, add_anno({valid, ValidIxs}, St1));
|
||||||
@@ -692,31 +817,49 @@ schemas_from_dynamic_eval(Obj, #st{s = Schema} = St) ->
|
|||||||
end;
|
end;
|
||||||
(<<"if">>, S, Acc) ->
|
(<<"if">>, S, Acc) ->
|
||||||
St1 = push_path('if', St),
|
St1 = push_path('if', St),
|
||||||
try valid(Obj, St1#st{s = S}) of
|
try valid(Obj, s(S, St1)) of
|
||||||
_ ->
|
_ ->
|
||||||
Sthen =
|
Sthen =
|
||||||
push_path(
|
push_path(
|
||||||
'then', St1#st{s = maps:get(<<"then">>, SMap, #{})}),
|
'then', s(maps:get(<<"then">>, SMap, #{}), St1)),
|
||||||
_ = valid(Obj, Sthen),
|
_ = valid(Obj, Sthen),
|
||||||
Acc ++ [Sthen]
|
Acc ++ [Sthen]
|
||||||
catch
|
catch
|
||||||
error:_ ->
|
error:_ ->
|
||||||
Selse =
|
Selse =
|
||||||
push_path(
|
push_path(
|
||||||
'else', St1#st{s = maps:get(<<"else">>, SMap, #{})}),
|
'else', s(maps:get(<<"else">>, SMap, #{}), St1)),
|
||||||
_ = valid(Obj, Selse),
|
_ = valid(Obj, Selse),
|
||||||
Acc ++ [Selse]
|
Acc ++ [Selse]
|
||||||
end;
|
end;
|
||||||
(<<"not">>, S, Acc) ->
|
(<<"not">>, S, Acc) ->
|
||||||
Snot = push_path('not', St#st{s = S}),
|
Snot = push_path('not', s(S, St)),
|
||||||
try valid(Obj, Snot) of
|
try valid(Obj, Snot) of
|
||||||
_ -> fail(invalid, Snot)
|
_ -> fail(invalid, Snot)
|
||||||
catch
|
catch
|
||||||
error:_ ->
|
error:_ ->
|
||||||
Acc
|
Acc
|
||||||
end;
|
end;
|
||||||
(_, _, Acc) -> Acc
|
(<<"x-", _/binary>> = Prop, SExt, Acc) ->
|
||||||
end, [], SMap).
|
case St#st.opts of
|
||||||
|
#{extensions := #{Prop := ExtF}} ->
|
||||||
|
St1 = push_path(Prop, St),
|
||||||
|
call_extension(ExtF, Obj, SExt, Prop, St1),
|
||||||
|
Acc;
|
||||||
|
_ ->
|
||||||
|
Acc
|
||||||
|
end;
|
||||||
|
(_, _, Acc) ->
|
||||||
|
Acc
|
||||||
|
end, [], SMap),
|
||||||
|
{Ss, St#st{d = Ss}}.
|
||||||
|
|
||||||
|
call_extension(F, Obj, S, Prop, St) ->
|
||||||
|
try F(Obj, S)
|
||||||
|
catch
|
||||||
|
error:E ->
|
||||||
|
fail(extended_check, add_anno({Prop, E}, St))
|
||||||
|
end.
|
||||||
|
|
||||||
acc_props(Ss) ->
|
acc_props(Ss) ->
|
||||||
lists:foldl(
|
lists:foldl(
|
||||||
@@ -728,6 +871,9 @@ acc_props(Ss) ->
|
|||||||
end
|
end
|
||||||
end, #{}, Ss).
|
end, #{}, Ss).
|
||||||
|
|
||||||
|
s(S, #st{} = St) ->
|
||||||
|
St#st{s = S, d = undefined}.
|
||||||
|
|
||||||
push_path(Ps, #st{p = P0} = St) when is_list(Ps) ->
|
push_path(Ps, #st{p = P0} = St) when is_list(Ps) ->
|
||||||
%% Assume Ps is in reverse order
|
%% Assume Ps is in reverse order
|
||||||
St#st{p = Ps ++ P0};
|
St#st{p = Ps ++ P0};
|
||||||
@@ -780,13 +926,15 @@ uniqueItems(L) ->
|
|||||||
USorted = lists:usort(L),
|
USorted = lists:usort(L),
|
||||||
[] == L -- USorted.
|
[] == L -- USorted.
|
||||||
|
|
||||||
expand_schema(S0) ->
|
expand_schema(S) ->
|
||||||
S = expand_definitions(S0),
|
%% S = expand_definitions(S0),
|
||||||
expand_schema(S, S).
|
expand_schema(S, S).
|
||||||
|
|
||||||
expand_definitions(#{<<"definitions">> := D} = S) ->
|
%% expand_definitions(#{<<"definitions">> := D} = S) ->
|
||||||
S#{<<"definitions">> := expand_schema(D, S)}.
|
%% S#{<<"definitions">> := expand_schema(D, S)}.
|
||||||
|
|
||||||
|
expand_schema(#{<<"$ref">> := Path} = V, S0) when map_size(V) == 1 ->
|
||||||
|
expand_schema(expand_ref(Path, S0), S0);
|
||||||
expand_schema(S, S0) when is_map(S) ->
|
expand_schema(S, S0) when is_map(S) ->
|
||||||
%% https://json-schema.org/understanding-json-schema/structuring#dollarref
|
%% https://json-schema.org/understanding-json-schema/structuring#dollarref
|
||||||
%% When $id is used in a subschema, it indicates an embedded schema.
|
%% When $id is used in a subschema, it indicates an embedded schema.
|
||||||
@@ -802,9 +950,9 @@ expand_schema(S, S0) when is_map(S) ->
|
|||||||
S0
|
S0
|
||||||
end,
|
end,
|
||||||
maps:fold(fun(K, V, Acc) -> expand_schema_(K, V, Acc, S1) end, #{}, S);
|
maps:fold(fun(K, V, Acc) -> expand_schema_(K, V, Acc, S1) end, #{}, S);
|
||||||
expand_schema([#{<<"$ref">> := Path} = V], S0) when map_size(V) == 1 ->
|
%% expand_schema([#{<<"$ref">> := Path} = V], S0) when map_size(V) == 1 ->
|
||||||
D = expand_ref(Path, S0),
|
%% D = expand_ref(Path, S0),
|
||||||
[D];
|
%% [expand_schema(D, S0)];
|
||||||
expand_schema(S, S0) when is_list(S) ->
|
expand_schema(S, S0) when is_list(S) ->
|
||||||
[expand_schema(E, S0) || E <- S];
|
[expand_schema(E, S0) || E <- S];
|
||||||
expand_schema(S, _) ->
|
expand_schema(S, _) ->
|
||||||
@@ -853,6 +1001,8 @@ schema(Path) ->
|
|||||||
schema(Path, Schema) ->
|
schema(Path, Schema) ->
|
||||||
schema(Path, Schema, #{follow_refs => true}).
|
schema(Path, Schema, #{follow_refs => true}).
|
||||||
|
|
||||||
|
schema(Path, #st{s = Schema, r = RootSchema}, Opts) ->
|
||||||
|
schema_(Path, Schema, RootSchema, Opts);
|
||||||
schema(Path, Schema, Opts) ->
|
schema(Path, Schema, Opts) ->
|
||||||
schema_(Path, Schema, Schema, Opts).
|
schema_(Path, Schema, Schema, Opts).
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,29 @@
|
|||||||
|
{
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"tx" : { "$ref" : "#/components/schemas/Tx" }
|
||||||
|
},
|
||||||
|
"components": {
|
||||||
|
"schemas": {
|
||||||
|
"Tx": {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"from": {
|
||||||
|
"allOf": [
|
||||||
|
{ "$ref": "#/components/schemas/Pubkey" },
|
||||||
|
{ "x-serialization": {
|
||||||
|
"tags": ["ak"]
|
||||||
|
}}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"Pubkey": {
|
||||||
|
"type": "string",
|
||||||
|
"x-serialization": {
|
||||||
|
"tags": ["ak", "ct"]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
{
|
||||||
|
"name": "Elizabeth",
|
||||||
|
"children": [
|
||||||
|
{
|
||||||
|
"name": "Charles",
|
||||||
|
"children": [
|
||||||
|
{
|
||||||
|
"name": "William",
|
||||||
|
"children": [
|
||||||
|
{ "name": "George" },
|
||||||
|
{ "name": "Charlotte" }
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Harry"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
{
|
||||||
|
"name": "Elizabeth",
|
||||||
|
"children": [
|
||||||
|
{
|
||||||
|
"name": "Charles",
|
||||||
|
"children": [
|
||||||
|
{
|
||||||
|
"name": "William",
|
||||||
|
"children": [
|
||||||
|
{ "name": [1] },
|
||||||
|
{ "name": "Charlotte" }
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Harry"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
{
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"name": { "type": "string" },
|
||||||
|
"children": {
|
||||||
|
"type": "array",
|
||||||
|
"items": { "$ref": "#" }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"$defs": {
|
||||||
|
"alice": { "$ref": "#/$defs/bob" },
|
||||||
|
"bob": { "$ref": "#/$defs/alice" }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,404 @@
|
|||||||
|
-module(gmconfig_schema_utils_tests).
|
||||||
|
|
||||||
|
-export([tr/0]).
|
||||||
|
|
||||||
|
-include_lib("eunit/include/eunit.hrl").
|
||||||
|
|
||||||
|
-import(gmconfig_schema_utils, [ valid/2
|
||||||
|
, validate/3 ]).
|
||||||
|
|
||||||
|
%%-define(t(Expr), ?_test(?debugVal(Expr))).
|
||||||
|
-define(t(Expr), ?_test(Expr)).
|
||||||
|
|
||||||
|
tr() ->
|
||||||
|
dbg:tracer(),
|
||||||
|
dbg:tpl(?MODULE, x),
|
||||||
|
dbg:tpl(gmconfig_schema_utils,x),
|
||||||
|
dbg:p(all,[c]),
|
||||||
|
eunit:test(?MODULE).
|
||||||
|
|
||||||
|
simple_type_test_() ->
|
||||||
|
{"Simple type tests",
|
||||||
|
[
|
||||||
|
?t(t_integer())
|
||||||
|
, ?t(t_number())
|
||||||
|
, ?t(t_boolean())
|
||||||
|
, ?t(t_string())
|
||||||
|
, ?t(t_array())
|
||||||
|
, ?t(t_object())
|
||||||
|
, ?t(t_shortcut_schema())
|
||||||
|
]}.
|
||||||
|
|
||||||
|
update_test_() ->
|
||||||
|
{"Validated update tests",
|
||||||
|
[
|
||||||
|
?t(t_update_objects())
|
||||||
|
]}.
|
||||||
|
|
||||||
|
schema_spec_examples_test_() ->
|
||||||
|
{"Examples from JSON-Schema docs",
|
||||||
|
[
|
||||||
|
?t(t_ref_loop())
|
||||||
|
, ?t(t_recursive_def())
|
||||||
|
, ?t(t_nested_refs())
|
||||||
|
]}.
|
||||||
|
|
||||||
|
array() -> #{<<"type">> => <<"array">>}.
|
||||||
|
int() -> #{<<"type">> => <<"integer">>}.
|
||||||
|
str() -> #{<<"type">> => <<"string">>}.
|
||||||
|
obj(Ps) -> #{<<"type">> => <<"object">>,
|
||||||
|
<<"properties">> => Ps}.
|
||||||
|
|
||||||
|
|
||||||
|
t_update_objects() ->
|
||||||
|
S0 = obj(
|
||||||
|
#{<<"a">> => int(),
|
||||||
|
<<"b">> => obj(#{<<"b1">> => array(),
|
||||||
|
<<"b2">> =>
|
||||||
|
obj(#{<<"b21">> => str()})
|
||||||
|
})
|
||||||
|
}),
|
||||||
|
%%
|
||||||
|
%% Only objects are merged; other types are replaced.
|
||||||
|
A0 = #{<<"a">> => 1,
|
||||||
|
<<"b">> => #{<<"b1">> => [1,2],
|
||||||
|
<<"b2">> => #{}}},
|
||||||
|
B0 = #{<<"a">> => 2,
|
||||||
|
<<"b">> => #{<<"b1">> => [4,5],
|
||||||
|
<<"b2">> => #{<<"b21">> => <<"foo">>}}},
|
||||||
|
#{<<"a">> := 1,
|
||||||
|
<<"b">> := #{<<"b1">> := [1,2],
|
||||||
|
<<"b2">> := #{<<"b21">> := <<"foo">>}}} =
|
||||||
|
gmconfig_schema_utils:merge(A0, B0, S0),
|
||||||
|
%%
|
||||||
|
%% Modified update semantics - replace instead of merge
|
||||||
|
S0Ps = maps:get(<<"properties">>, S0),
|
||||||
|
S01Ps = S0Ps#{<<"c">> => #{<<"type">> => <<"object">>,
|
||||||
|
<<"updateSemantics">> => <<"replace">>,
|
||||||
|
<<"properties">> =>
|
||||||
|
#{<<"c1">> => #{<<"type">> => <<"integer">>},
|
||||||
|
<<"c2">> => #{<<"type">> => <<"integer">>}
|
||||||
|
}}},
|
||||||
|
S01 = S0#{<<"properties">> => S01Ps},
|
||||||
|
A01 = A0#{<<"c">> => #{<<"c1">> => 1}},
|
||||||
|
B01 = B0#{<<"c">> => #{<<"c2">> => 2}},
|
||||||
|
#{<<"a">> := 1,
|
||||||
|
<<"b">> := #{<<"b1">> := [1,2],
|
||||||
|
<<"b2">> := #{<<"b21">> := <<"foo">>}},
|
||||||
|
<<"c">> := #{<<"c1">> := 1}} =
|
||||||
|
gmconfig_schema_utils:merge(A01, B01, S01),
|
||||||
|
A0r = #{<<"a">> => 1,
|
||||||
|
<<"b">> => #{<<"$updateSemantics">> => <<"replace">>,
|
||||||
|
<<"b1">> => [1,2],
|
||||||
|
<<"b2">> => #{<<"$updateSemantics">> => <<"merge">>}
|
||||||
|
}},
|
||||||
|
#{<<"a">> := 1,
|
||||||
|
<<"b">> := #{<<"b1">> := [1,2],
|
||||||
|
<<"b2">> := B2M}} =
|
||||||
|
gmconfig_schema_utils:merge(A0r, B0, S0),
|
||||||
|
%% B2M is the empty map (updateSemantics prop removed recursively)
|
||||||
|
?assertEqual(#{}, B2M),
|
||||||
|
A0r1 = #{<<"a">> => 1,
|
||||||
|
<<"b">> => #{<<"$updateSemantics">> => <<"merge">>,
|
||||||
|
<<"b1">> => [1,2],
|
||||||
|
<<"b2">> => #{<<"$updateSemantics">> => <<"replace">>}
|
||||||
|
}},
|
||||||
|
#{<<"a">> := 1,
|
||||||
|
<<"b">> := #{<<"b1">> := [1,2],
|
||||||
|
<<"b2">> := B2M1}} =
|
||||||
|
gmconfig_schema_utils:merge(A0r1, B0, S0),
|
||||||
|
%% B2M is the empty map (updateSemantics prop removed recursively)
|
||||||
|
?assertEqual(#{}, B2M1),
|
||||||
|
%%
|
||||||
|
%% With updateSemantics : suggest, the offered data is accepted if
|
||||||
|
%% the existing is missing or 'null'.
|
||||||
|
S02Ps = S0Ps#{<<"c">> => #{<<"type">> => <<"object">>,
|
||||||
|
<<"updateSemantics">> => <<"suggest">>,
|
||||||
|
<<"properties">> =>
|
||||||
|
#{<<"c1">> => #{<<"type">> => <<"integer">>},
|
||||||
|
<<"c2">> => #{<<"type">> => <<"integer">>}
|
||||||
|
}}},
|
||||||
|
S02 = S0#{<<"properties">> => S02Ps},
|
||||||
|
A02 = A0#{<<"c">> => #{<<"c1">> => 1}},
|
||||||
|
B02 = B0#{<<"c">> => #{<<"c2">> => 2}},
|
||||||
|
#{<<"a">> := 1,
|
||||||
|
<<"b">> := #{<<"b1">> := [1,2],
|
||||||
|
<<"b2">> := #{<<"b21">> := <<"foo">>}},
|
||||||
|
<<"c">> := #{<<"c2">> := 2}} =
|
||||||
|
gmconfig_schema_utils:merge(A02, B02, S02),
|
||||||
|
B03 = B0#{<<"c">> => null},
|
||||||
|
#{<<"a">> := 1,
|
||||||
|
<<"b">> := #{<<"b1">> := [1,2],
|
||||||
|
<<"b2">> := #{<<"b21">> := <<"foo">>}},
|
||||||
|
<<"c">> := #{<<"c1">> := 1}} =
|
||||||
|
gmconfig_schema_utils:merge(A02, B0, S02),
|
||||||
|
#{<<"a">> := 1,
|
||||||
|
<<"b">> := #{<<"b1">> := [1,2],
|
||||||
|
<<"b2">> := #{<<"b21">> := <<"foo">>}},
|
||||||
|
<<"c">> := #{<<"c1">> := 1}} =
|
||||||
|
gmconfig_schema_utils:merge(A02, B03, S02),
|
||||||
|
ok.
|
||||||
|
|
||||||
|
t_integer() ->
|
||||||
|
S0 = #{<<"type">> => <<"integer">>},
|
||||||
|
is_valid(17, S0),
|
||||||
|
all_fail([17.0, [], #{}, <<>>], S0, wrong_type),
|
||||||
|
S1 = S0#{<<"minimum">> => 1, <<"maximum">> => 4},
|
||||||
|
all_valid([1, 2, 4], S1),
|
||||||
|
all_fail([0, 5], S1, not_in_range),
|
||||||
|
S2 = S0#{<<"exclusiveMinimum">> => 1, <<"exclusiveMaximum">> => 3},
|
||||||
|
is_valid(2, S2),
|
||||||
|
all_fail([1, 3], S2, not_in_range),
|
||||||
|
S3 = S0#{<<"multipleOf">> => 2},
|
||||||
|
is_valid(4, S3),
|
||||||
|
fails(3, S3, not_a_multiple),
|
||||||
|
ok.
|
||||||
|
|
||||||
|
t_number() ->
|
||||||
|
S0 = #{<<"type">> => <<"number">>},
|
||||||
|
all_valid([17, 17.0], S0),
|
||||||
|
all_fail([#{}, [], <<>>, true], S0, wrong_type),
|
||||||
|
S1 = S0#{<<"minimum">> => 1, <<"maximum">> => 4.0},
|
||||||
|
all_valid([1, 1.0, 2, 2.3, 3.999, 4, 4.0], S1),
|
||||||
|
all_fail([0, 5], S1, not_in_range),
|
||||||
|
S2 = S0#{<<"exclusiveMinimum">> => 1, <<"exclusiveMaximum">> => 3},
|
||||||
|
is_valid(2, S2),
|
||||||
|
all_fail([1, 3], S2, not_in_range),
|
||||||
|
S3 = S0#{<<"multipleOf">> => 2},
|
||||||
|
is_valid(4, S3),
|
||||||
|
all_fail([3, 4.0], S3, not_a_multiple),
|
||||||
|
ok.
|
||||||
|
|
||||||
|
t_boolean() ->
|
||||||
|
S0 = #{<<"type">> => <<"boolean">>},
|
||||||
|
all_valid([true, false], S0),
|
||||||
|
%% truthy and falsy types not accepted
|
||||||
|
all_fail([1, 0, null, [], <<>>, #{}], S0, wrong_type),
|
||||||
|
ok.
|
||||||
|
|
||||||
|
t_string() ->
|
||||||
|
S0 = #{<<"type">> => <<"string">>},
|
||||||
|
all_valid([<<>>, <<"foo">>], S0),
|
||||||
|
fails("foo", S0, wrong_type),
|
||||||
|
ok.
|
||||||
|
|
||||||
|
t_array() ->
|
||||||
|
S0 = #{<<"type">> => <<"array">>},
|
||||||
|
all_valid([ [], [1,1], [1,2,3], [#{}] ], S0),
|
||||||
|
all_fail([1, false, #{}, <<"foo">>], S0, wrong_type),
|
||||||
|
S1 = S0#{<<"minItems">> => 2},
|
||||||
|
all_valid([[1,2], [1,2,3,4]], S1),
|
||||||
|
all_fail([[], [1]], S1, not_in_range),
|
||||||
|
S2 = S0#{<<"maxItems">> => 4},
|
||||||
|
all_valid([[], [1], [1,2], [1,2,3,4]], S2),
|
||||||
|
fails([1,2,3,4,5], S2, not_in_range),
|
||||||
|
S3 = maps:merge(S1, S2),
|
||||||
|
all_valid([ [1,2], [1,2,3], [1,2,3,4] ], S3),
|
||||||
|
all_fail([ [1], [1,2,3,4,5] ], S3, not_in_range),
|
||||||
|
S4 = S0#{<<"uniqueItems">> => true},
|
||||||
|
all_valid([ [], [1], [1,2] ], S4),
|
||||||
|
all_fail([ [1,1], [2,1,2] ], S4, not_unique),
|
||||||
|
S5 = S0#{<<"items">> => #{<<"type">> => <<"integer">>}},
|
||||||
|
all_valid([ [], [1], [1,2,3] ], S5),
|
||||||
|
fails([<<"foo">>], S5, #{e => wrong_type, p => [array,items,0,integer]}),
|
||||||
|
fails([1,2,false], S5, #{e => wrong_type, p => [array,items,2,integer]}),
|
||||||
|
S6 = S0#{<<"prefixItems">> => [#{<<"type">> => <<"string">>},
|
||||||
|
#{<<"type">> => <<"integer">>},
|
||||||
|
#{<<"type">> => <<"integer">>}]},
|
||||||
|
is_valid([<<"foo">>, 1], S6),
|
||||||
|
is_valid([<<"foo">>, 1, 2], S6),
|
||||||
|
is_valid([<<"foo">>, 1, 2, 3], S6),
|
||||||
|
fails([1,2,3], S6, #{e => wrong_type, p => [array,prefixItems,0,string]}),
|
||||||
|
S7 = S6#{<<"items">> => false},
|
||||||
|
fails([<<"foo">>, 1, 2, 3], S7, #{e => invalid,
|
||||||
|
p => [array,prefixItems,items,3]}),
|
||||||
|
S71 = S6#{<<"items">> => #{<<"type">> => <<"integer">>}},
|
||||||
|
is_valid([<<>>, 1,2,3,4,5,6], S71),
|
||||||
|
fails([<<>>, 1,2,3,4,5,[]], S71,
|
||||||
|
#{e => wrong_type,
|
||||||
|
p => [array,prefixItems,items,6,integer]}),
|
||||||
|
S8 = S0#{<<"contains">> => #{<<"type">> => <<"integer">>}},
|
||||||
|
is_valid([1,2,3], S8),
|
||||||
|
is_valid([true,true,1], S8),
|
||||||
|
fails([true], S8, contains),
|
||||||
|
fails([], S8, contains), % not sure where specifed, but makes sense
|
||||||
|
S81 = S8#{<<"minContains">> => 2},
|
||||||
|
is_valid([1,2,3], S81),
|
||||||
|
is_valid([true,1,2], S81),
|
||||||
|
fails([1], S81, #{e => not_in_range, p => [array, contains, min,
|
||||||
|
integer, minimum]}),
|
||||||
|
S82 = S8#{<<"maxContains">> => 4},
|
||||||
|
all_valid([ [1], [1,2,3], [true,1,2,3,4] ], S82),
|
||||||
|
S83 = maps:merge(S81, S82),
|
||||||
|
is_valid([1,2,3], S83),
|
||||||
|
fails([1], S83, #{e => not_in_range, p => [array, contains, min,
|
||||||
|
integer, minimum]}),
|
||||||
|
fails([1,2,3,4,5], S83, #{e => not_in_range, p => [array, contains, max,
|
||||||
|
integer, maximum]}),
|
||||||
|
is_valid([], S8#{<<"minContains">> => 0}),
|
||||||
|
ok.
|
||||||
|
|
||||||
|
t_object() ->
|
||||||
|
S0 = #{<<"type">> => <<"object">>},
|
||||||
|
all_valid([ #{}, #{<<"a">> => 1} ], S0),
|
||||||
|
all_fail([ true, 17, 17.0, [], <<"foo">> ], S0, #{e => wrong_type}),
|
||||||
|
S1 = S0#{<<"properties">> => #{<<"a">> => #{<<"type">> => <<"integer">>}}},
|
||||||
|
all_valid([ #{<<"a">> => 1}, #{}, #{<<"b">> => 1} ], S1),
|
||||||
|
fails(#{<<"a">> => true}, S1, #{e => wrong_type,
|
||||||
|
p => [object, <<"a">>, integer]}),
|
||||||
|
is_valid(#{<<"b">> => 1}, S1#{<<"additionalProperties">> => true}),
|
||||||
|
fails(#{<<"b">> => 1}, S1#{<<"additionalProperties">> => false},
|
||||||
|
#{e => invalid,
|
||||||
|
p => [object, <<"b">>],
|
||||||
|
a => [additionalProperties]}),
|
||||||
|
fails(#{<<"b">> => 1}, S1#{<<"required">> => [<<"a">>]},
|
||||||
|
#{e => required, p => [object,required], a => [[<<"a">>]]}),
|
||||||
|
all_valid([ #{<<"a">> => 1}, #{<<"a">> => true} ],
|
||||||
|
S0#{<<"properties">> =>
|
||||||
|
#{<<"a">> => #{<<"oneOf">> =>
|
||||||
|
[#{<<"type">> => <<"integer">>},
|
||||||
|
#{<<"type">> => <<"boolean">>}]}
|
||||||
|
}}),
|
||||||
|
S2 = S0#{<<"properties">> =>
|
||||||
|
#{<<"a">> => #{<<"type">> => <<"integer">>,
|
||||||
|
<<"allOf">> =>
|
||||||
|
[#{<<"minimum">> => 2},
|
||||||
|
#{<<"maximum">> => 5},
|
||||||
|
#{<<"multipleOf">> => 2}
|
||||||
|
]}}
|
||||||
|
},
|
||||||
|
is_valid(#{<<"a">> => 4}, S2),
|
||||||
|
fails(#{<<"a">> => 3}, S2, #{e => failing_schemas,
|
||||||
|
a => [[{2,'_'}]|'_'],
|
||||||
|
p => [object, <<"a">>, allOf]}),
|
||||||
|
ok.
|
||||||
|
|
||||||
|
t_shortcut_schema() ->
|
||||||
|
Vs = [<<"foo">>,
|
||||||
|
#{<<"a">> => 1},
|
||||||
|
17],
|
||||||
|
all_valid(Vs, #{}),
|
||||||
|
all_valid(Vs, true),
|
||||||
|
all_fail(Vs, false, invalid),
|
||||||
|
ok.
|
||||||
|
|
||||||
|
is_valid(V, S) ->
|
||||||
|
{V, V} = {valid(V, S), V}.
|
||||||
|
%% ?assertEqual(V, valid(V, S)).
|
||||||
|
|
||||||
|
fails(V, S, Reason) ->
|
||||||
|
fails(V, S, #{}, Reason).
|
||||||
|
|
||||||
|
fails(V, S, Opts, Reason) when is_atom(Reason) ->
|
||||||
|
fails(V, S, Opts, #{e => Reason});
|
||||||
|
fails(V, S, Opts, Expect) ->
|
||||||
|
try validate(V, S, Opts) of
|
||||||
|
_ ->
|
||||||
|
error({expected_exception, #{v => V,
|
||||||
|
s => S,
|
||||||
|
e => Expect}})
|
||||||
|
catch
|
||||||
|
error:R ->
|
||||||
|
match_expected(Expect, R)
|
||||||
|
end.
|
||||||
|
%% ?assertError({Reason, [], V}, valid(V, S)).
|
||||||
|
|
||||||
|
match_expected(E, R) ->
|
||||||
|
case maps:fold(
|
||||||
|
fun(K, V, Acc) ->
|
||||||
|
case maps:find(K, R) of
|
||||||
|
{ok, V} -> Acc;
|
||||||
|
{ok, Other} ->
|
||||||
|
case match_other(V, Other) of
|
||||||
|
true -> Acc;
|
||||||
|
false ->
|
||||||
|
Acc#{K => Other}
|
||||||
|
end;
|
||||||
|
error ->
|
||||||
|
Acc#{K => '$not_found'}
|
||||||
|
end
|
||||||
|
end, #{}, E) of
|
||||||
|
M when map_size(M) == 0 ->
|
||||||
|
ok;
|
||||||
|
Unexpected ->
|
||||||
|
error({expected, E, Unexpected})
|
||||||
|
end.
|
||||||
|
|
||||||
|
match_other(V, V) -> true;
|
||||||
|
match_other([H1|T1], [H2|T2]) ->
|
||||||
|
case match_other(H1, H2) of
|
||||||
|
true ->
|
||||||
|
match_other(T1, T2);
|
||||||
|
false ->
|
||||||
|
false
|
||||||
|
end;
|
||||||
|
match_other(T1, T2) when tuple_size(T1) =:= tuple_size(T2) ->
|
||||||
|
lists:all(fun({A,B}) -> match_other(A, B) end,
|
||||||
|
lists:zip(tuple_to_list(T1),
|
||||||
|
tuple_to_list(T2)));
|
||||||
|
match_other('_', _) ->
|
||||||
|
true;
|
||||||
|
match_other(_, _) ->
|
||||||
|
false.
|
||||||
|
|
||||||
|
all_valid(Vs, S) ->
|
||||||
|
[is_valid(V, S) || V <- Vs].
|
||||||
|
|
||||||
|
all_fail(Vs, S, Reason) ->
|
||||||
|
[fails(V, S, Reason) || V <- Vs].
|
||||||
|
|
||||||
|
read(F) ->
|
||||||
|
FullF = filename:join(
|
||||||
|
filename:dirname(code:which(?MODULE)), F),
|
||||||
|
?debugFmt("FullF = ~s~n", [FullF]),
|
||||||
|
{ok, Bin} = file:read_file(FullF),
|
||||||
|
dec(Bin).
|
||||||
|
|
||||||
|
dec(Str) ->
|
||||||
|
try json:decode(Str)
|
||||||
|
catch
|
||||||
|
error:E ->
|
||||||
|
error(#{type => json_decode_error,
|
||||||
|
error => E})
|
||||||
|
end.
|
||||||
|
|
||||||
|
|
||||||
|
t_ref_loop() ->
|
||||||
|
S = read("data/ref_loop_schema.json"),
|
||||||
|
try gmconfig_schema_utils:schema([<<"$defs">>,<<"alice">>], S) of
|
||||||
|
Unexpected ->
|
||||||
|
error({unexpected, Unexpected})
|
||||||
|
catch
|
||||||
|
error:nested_references ->
|
||||||
|
ok
|
||||||
|
end.
|
||||||
|
|
||||||
|
t_recursive_def() ->
|
||||||
|
S = read("data/recursion_schema.json"),
|
||||||
|
D = read("data/recursion_data.json"),
|
||||||
|
Df = read("data/recursion_data_faulty.json"),
|
||||||
|
is_valid(D, S),
|
||||||
|
fails(Df, S, #{e => wrong_type, p => [object, <<"children">>,
|
||||||
|
array, items, 0,
|
||||||
|
object, <<"children">>,
|
||||||
|
array, items, 0,
|
||||||
|
object,<<"children">>,
|
||||||
|
array, items, 0,
|
||||||
|
object, <<"name">>, string
|
||||||
|
]}),
|
||||||
|
ok.
|
||||||
|
|
||||||
|
t_nested_refs() ->
|
||||||
|
S = read("data/nested_refs_schema.json"),
|
||||||
|
F = fun(Str, #{<<"tags">> := Tags}) ->
|
||||||
|
true = lists:any(
|
||||||
|
fun(T) ->
|
||||||
|
nomatch =/= string:prefix(Str, T)
|
||||||
|
end, Tags)
|
||||||
|
end,
|
||||||
|
Opts = #{extensions => #{<<"x-serialization">> => F}},
|
||||||
|
Vs = #{<<"tx">> => #{<<"from">> => <<"ak_good">>}},
|
||||||
|
Vf = #{<<"tx">> => #{<<"from">> => <<"ac_bad">>}},
|
||||||
|
validate(Vs, S, Opts),
|
||||||
|
fails(Vf, S, Opts, #{e => failing_schemas}),
|
||||||
|
ok.
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
{name,"gmconfig"}.
|
||||||
|
{type,app}.
|
||||||
|
{modules,[]}.
|
||||||
|
{prefix,"gmconfig"}.
|
||||||
|
{author,"Ulf Wiger"}.
|
||||||
|
{desc,"Configuration management support"}.
|
||||||
|
{package_id,{"uwiger","gmconfig",{0,2,0}}}.
|
||||||
|
{deps,[{"uwiger","setup",{2,2,4}}]}.
|
||||||
|
{key_name,none}.
|
||||||
|
{a_email,"ulf@wiger.net"}.
|
||||||
|
{c_email,"ulf@wiger.net"}.
|
||||||
|
{copyright,"Ulf Wiger"}.
|
||||||
|
{file_exts,[]}.
|
||||||
|
{license,"GPL-3.0-or-later"}.
|
||||||
|
{repo_url,"https://git.qpq.swiss/QPQ-AG/gmconfig"}.
|
||||||
|
{tags,[]}.
|
||||||
|
{ws_url,[]}.
|
||||||
Executable
+42
@@ -0,0 +1,42 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
set -e
|
||||||
|
|
||||||
|
APP=$(basename "$PWD")
|
||||||
|
|
||||||
|
SRC="_build/default/lib/$APP"
|
||||||
|
DST="$PWD/_build/zomp/lib/$APP"
|
||||||
|
IGNORE_FILE="zomp.ignore"
|
||||||
|
|
||||||
|
mkdir -p "$DST"
|
||||||
|
|
||||||
|
# Remove broken symlinks
|
||||||
|
find "$SRC" -type l ! -exec test -e {} \; -delete || true
|
||||||
|
|
||||||
|
# Build ignore matcher
|
||||||
|
IGNORE_TEMP=$(mktemp)
|
||||||
|
trap "rm -f $IGNORE_TEMP" EXIT
|
||||||
|
|
||||||
|
# Expand globs in zomp.ignore to patterns suitable for grep
|
||||||
|
if [ -e "$IGNORE_FILE" ]; then
|
||||||
|
grep -v '^\s*#' "$IGNORE_FILE" | sed 's#/#\\/#g' | sed 's/\./\\./g' | sed 's/\*/.*/g' > "$IGNORE_TEMP"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Copy Git-tracked and Zomp-allowed files
|
||||||
|
git ls-files -z | while IFS= read -r -d '' file; do
|
||||||
|
# Skip if ignored
|
||||||
|
echo "$file" | grep -Eq -f "$IGNORE_TEMP" && continue
|
||||||
|
# Only copy if file exists in the build dir
|
||||||
|
if [ -e "$SRC/$file" ]; then
|
||||||
|
mkdir -p "$DST/$(dirname "$file")"
|
||||||
|
cp -a "$SRC/$file" "$DST/$file"
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
rm "$IGNORE_TEMP"
|
||||||
|
|
||||||
|
# Copy metadata
|
||||||
|
cp "$PWD/zomp.meta" "$DST/"
|
||||||
|
cp "$PWD/Emakefile" "$DST/"
|
||||||
|
|
||||||
|
# Clean up beam files just in case
|
||||||
|
[ -d "$DST/ebin" ] && find "$DST/ebin" -name '*.beam' -exec rm -f {} + || true
|
||||||
Reference in New Issue
Block a user