Skip to content

Commit

Permalink
Trim down contracts (v1.29.3)
Browse files Browse the repository at this point in the history
Use the new (currently unmerged) `--library-path` argument of
json-schema-to-nickel to avoid inlining the predicate library in each
and every contract, and shared it as `.js2n-lib` instead, reducing the
size of provided contract - at the cost of making them not standalone.
  • Loading branch information
yannham committed Apr 19, 2024
1 parent 55d2dbb commit 6e32565
Show file tree
Hide file tree
Showing 1,084 changed files with 979,825 additions and 1,736,665 deletions.
15 changes: 12 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,23 @@ Kubernetes resources and Nickel modules.

Although the repository was created several years ago, this project started as
an experiment which quickly came to an halt when the initial contributor
couldn't devote time to it anymore. However, it just has been revived.
Nickel-kubernetes is under active development, but it isn't ready to use yet.
couldn't devote time to it anymore. However, it has been revived since and is
under active development.

## Content

You'll find auto-generated Nickel contracts for Kubernetes resources for some
selected versions of the Kubernetes API in the `kubernetes-contracts` directory.
You can simply import them and apply them to your Nickel configurations.

See the roadmap below for the next steps.
**IMPORTANT**: the generated contracts are all implicitly relying on a Nickel
library which is located inside each contract directory
`kubernetes-contracts/vX.Y.Z` in the `.js2n-lib` subdirectory. If you want to
use only a few selected contracts without pulling the whole contract directory
in, make sure to copy the library as well (which must either be located in the
same directory as the contracts or must be made available through the Nickel
import path - see the documentation of `--import-path` by running `nickel help
export`).

## Roadmap

Expand Down
166 changes: 166 additions & 0 deletions kubernetes-contracts/v1.29.3/.js2n-lib/arrays.ncl
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
{
arrayOf
: (Dyn -> { success : Bool, error : String }) -> Dyn -> { success : Bool, error : String }
| doc m%"
Apply a predicate to all elements of an array, succeeding if all
applications succeed. If the value isn't an array, fail.
"%
= fun pred x =>
if !std.is_array x then
{ success = true, error = "" }
else
let x = x | Array Dyn in
x
|> std.array.fold_right
(
fun x acc =>
let result = pred x in
if !result.success then
result
else
acc
)
{ success = true, error = "" },

contains
: (Dyn -> { success : Bool, error : String }) -> Dyn -> { success : Bool, error : String }
| doc m%"
Cf. [https://datatracker.ietf.org/doc/html/draft-handrews-json-schema-validation-01#section-6.4.6]
"%
= fun pred x =>
if !std.is_array x then
{ success = true, error = "" }
else
let x = x | Array Dyn in
x
|> std.array.fold_right
(
fun x acc =>
let result = pred x in
if result.success || acc.success then
{ success = true, errors = [] }
else
{ success = false, errors = [" - %{result.error}"] @ acc.errors }
)
{ success = false, errors = [] }
|> (
fun result =>
if result.success then
{ success = true, error = "" }
else
{
success = false,
error = "contains: no elements matched\n%{std.string.join "\n" result.errors}"
}
),

items
: Array (Dyn -> { success : Bool, error : String }) -> Dyn -> { success : Bool, error : String }
| doc m%"
Cf. [https://datatracker.ietf.org/doc/html/draft-handrews-json-schema-validation-01#section-6.4.1]
"%
= fun preds x =>
if !std.is_array x then
{ success = true, error = "" }
else
let x = x | Array Dyn in
let length_to_check = std.number.min (std.array.length preds) (std.array.length x) in
std.array.range 0 length_to_check
|> std.array.fold_right
(
fun i acc =>
let result = (std.array.at i preds) (std.array.at i x) in
if !result.success then
result
else
acc
)
{ success = true, error = "" },

additionalItems
: (Dyn -> { success : Bool, error : String }) -> Number -> Dyn -> { success : Bool, error : String }
| doc m%"
Cf. [https://datatracker.ietf.org/doc/html/draft-handrews-json-schema-validation-01#section-6.4.2]
"%
= fun pred start x =>
if !std.is_array x then
{ success = true, error = "" }
else
let x = x | Array Dyn in
let value_length = std.array.length x in
if start >= value_length then
{ success = true, error = "" }
else
(std.array.slice start value_length x | Dyn)
|> arrayOf pred,

maxItems
: Number -> Dyn -> { success : Bool, error : String }
| doc m%"
`maxItems n x` fails if `x` is an array of length strictly greater than `n` and succeeds otherwise.
Cf. [https://datatracker.ietf.org/doc/html/draft-handrews-json-schema-validation-01#section-6.4.3]
"%
= fun n x =>
if !std.is_array x then
{ success = true, error = "" }
else if std.array.length (x | Array Dyn) > n then
{ success = false, error = "array is longer than %{std.string.from_number n} items" }
else
{ success = true, error = "" },

minItems
: Number -> Dyn -> { success : Bool, error : String }
| doc m%"
`minItems n x` fails if `x` is an array of length strictly smaller than `n` and succeeds otherwise.
Cf. [https://datatracker.ietf.org/doc/html/draft-handrews-json-schema-validation-01#section-6.4.4]
"%
= fun n x =>
if !std.is_array x then
{ success = true, error = "" }
else if std.array.length (x | Array Dyn) < n then
{ success = false, error = "array is shorter than %{std.string.from_number n} items" }
else
{ success = true, error = "" },

uniqueItems
: Dyn -> { success : Bool, error : String }
| doc m%"
Succeeds for any array if its elements are pairwise distinct.
Cf. [https://datatracker.ietf.org/doc/html/draft-handrews-json-schema-validation-01#section-6.4.5]
"%
=
let find_duplicate
: Array Dyn -> { has_duplicate : Bool, duplicate : Dyn, seen : { _ : Bool } }
| doc m%"
Try to find a duplicate in an array of Nickel values. For
efficienyc, this function uses JSON serialization to index into
a Nickel record. Consequently all values in the array must be
serializable to JSON.
"%
=
std.array.fold_right
(
fun elt acc =>
if acc.has_duplicate then
acc
else
let index = std.serialize 'Json elt in
if std.record.has_field index acc.seen then
{ has_duplicate = true, duplicate = elt, seen = acc.seen }
else
{ has_duplicate = false, duplicate = null, seen = std.record.insert index true acc.seen }
)
{ has_duplicate = false, duplicate = null, seen = {} }
in
fun x =>
if !std.is_array x then
{ success = true, error = "" }
else
let { has_duplicate, duplicate, .. } = find_duplicate (x | Array Dyn)
in
if has_duplicate then
{ success = false, error = "duplicate found: %{std.serialize 'Json duplicate}" }
else
{ success = true, error = "" },
}

67 changes: 67 additions & 0 deletions kubernetes-contracts/v1.29.3/.js2n-lib/numbers.ncl
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{
multipleOf
: Number -> Dyn -> { success : Bool, error : String }
| doc m%"
Cf. [https://datatracker.ietf.org/doc/html/draft-handrews-json-schema-validation-01#section-6.2.1]
"%
= fun mult x =>
if !std.is_number x then
{ success = true, error = "" }
else if std.number.is_integer ((x | Number) / mult) then
{ success = true, error = "" }
else
{ success = false, error = "expected a multiple of %{std.string.from_number mult}" },

maximum
: Number -> Dyn -> { success : Bool, error : String }
| doc m%"
Cf. [https://datatracker.ietf.org/doc/html/draft-handrews-json-schema-validation-01#section-6.2.2]
"%
= fun limit x =>
if !std.is_number x then
{ success = true, error = "" }
else if (x | Number) <= limit then
{ success = true, error = "" }
else
{ success = false, error = "expected a maximum of %{std.string.from_number limit}" },

exclusiveMaximum
: Number -> Dyn -> { success : Bool, error : String }
| doc m%"
Cf. [https://datatracker.ietf.org/doc/html/draft-handrews-json-schema-validation-01#section-6.2.3]
"%
= fun limit x =>
if !std.is_number x then
{ success = true, error = "" }
else if (x | Number) < limit then
{ success = true, error = "" }
else
{ success = false, error = "expected an exclusive maximum of %{std.string.from_number limit}" },

minimum
: Number -> Dyn -> { success : Bool, error : String }
| doc m%"
Cf. [https://datatracker.ietf.org/doc/html/draft-handrews-json-schema-validation-01#section-6.2.4]
"%
= fun limit x =>
if !std.is_number x then
{ success = true, error = "" }
else if (x | Number) >= limit then
{ success = true, error = "" }
else
{ success = false, error = "expected a minimum of %{std.string.from_number limit}" },

exclusiveMinimum
: Number -> Dyn -> { success : Bool, error : String }
| doc m%"
Cf. [https://datatracker.ietf.org/doc/html/draft-handrews-json-schema-validation-01#section-6.2.5]
"%
= fun limit x =>
if !std.is_number x then
{ success = true, error = "" }
else if (x | Number) > limit then
{ success = true, error = "" }
else
{ success = false, error = "expected an exclusive minimum of %{std.string.from_number limit}" },
}

Loading

0 comments on commit 6e32565

Please sign in to comment.