Skip to content

Commit

Permalink
Merge branch 'master' into feat/jsonml
Browse files Browse the repository at this point in the history
  • Loading branch information
sparkprime authored Jun 13, 2023
2 parents 471d4ab + a5fefdb commit 0c3ff5b
Show file tree
Hide file tree
Showing 6 changed files with 181 additions and 5 deletions.
53 changes: 53 additions & 0 deletions .github/workflows/create_archive_and_notes.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/usr/bin/env bash
# Copyright 2023 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -o errexit -o nounset -o pipefail

# Set by GH actions, see
# https://docs.github.com/en/actions/learn-github-actions/environment-variables#default-environment-variables
TAG=${GITHUB_REF_NAME}
# A prefix is added to better match the GitHub generated archives.
PREFIX="jsonnet-${TAG}"
ARCHIVE="jsonnet-$TAG.tar.gz"
git archive --format=tar --prefix=${PREFIX}/ ${TAG} | gzip > $ARCHIVE
SHA=$(shasum -a 256 $ARCHIVE | awk '{print $1}')

cat > release_notes.txt << EOF
### Importing Jsonnet in a project that uses Bazel
#### Using Bzlmod with Bazel 6
Add to your \`MODULE.bazel\` file:
\`\`\`starlark
bazel_dep(name = "jsonnet", version = "${TAG}")
\`\`\`
#### Using WORKSPACE
Paste this snippet into your \`WORKSPACE\` file:
\`\`\`starlark
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
name = "jsonnet",
sha256 = "${SHA}",
strip_prefix = "${PREFIX}",
url = "https://github.com/google/jsonnet/releases/download/${TAG}/jsonnet-${TAG}.tar.gz",
)
\`\`\`
EOF
38 changes: 38 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Copyright 2023 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Cut a release whenever a new tag is pushed to the repo.
name: Release

on:
push:
tags:
- "*.*.*"

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Create release archive and notes
run: .github/workflows/create_archive_and_notes.sh
- name: Release
uses: softprops/action-gh-release@v1
with:
# Use GH feature to populate the changelog automatically
generate_release_notes: true
body_path: release_notes.txt
fail_on_unmatched_files: true
files: jsonnet-*.tar.gz
6 changes: 2 additions & 4 deletions MODULE.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module(name = "jsonnet", version = "0.0.0")

bazel_dep(name = "googletest", version = "1.11.0", repo_name = "com_google_googletest")

build_defs = use_extension("//tools/build_defs:extensions.bzl", "build_defs")
use_repo(
build_defs,
Expand All @@ -8,7 +10,3 @@ use_repo(
)

register_toolchains("//platform_defs:default_python3_toolchain")

# Dev dependencies

bazel_dep(name = "googletest", version = "1.11.0", repo_name = "com_google_googletest", dev_dependency = True)
44 changes: 44 additions & 0 deletions doc/_stdlib_gen/stdlib-content.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,20 @@ local html = import 'html.libsonnet';
<ul><code>std.acos(x)</code></ul>
<ul><code>std.atan(x)</code></ul>
<ul><code>std.round(x)</code></ul>
<ul><code>std.isEven(x)</code></ul>
<ul><code>std.isOdd(x)</code></ul>
<ul><code>std.isInteger(x)</code></ul>
<ul><code>std.isDecimal(x)</code></ul>
</ul>
<p>
The function <code>std.mod(a, b)</code> is what the % operator is desugared to. It performs
modulo arithmetic if the left hand side is a number, or if the left hand side is a string,
it does Python-style string formatting with <code>std.format()</code>.
</p>
<p>
The functions <code>std.isEven(x)</code> and <code>std.isOdd(x)</code> use integral part of a
floating number to test for even or odd.
</p>
|||,
],
fields: [
Expand Down Expand Up @@ -371,6 +379,14 @@ local html = import 'html.libsonnet';
Returns true if the the given string is of zero length.
|||,
},
{
name: 'trim',
params: ['str'],
availableSince: 'upcoming',
description: |||
Returns a copy of string after eliminating leading and trailing whitespaces.
|||,
},
{
name: 'equalsIgnoreCase',
params: ['str1', 'str2'],
Expand Down Expand Up @@ -1303,6 +1319,20 @@ local html = import 'html.libsonnet';
},
],
},
{
name: 'flattenDeepArray',
params: ['value'],
availableSince: 'upcoming',
description: |||
Concatenate an array containing values and arrays into a single flattened array.
|||,
examples: [
{
input: 'std.flattenDeepArray([[1, 2], [], [3, [4]], [[5, 6, [null]], [7, 8]]])',
output: std.flattenDeepArray([[1, 2], [], [3, [4]], [[5, 6, [null]], [7, 8]]]),
},
],
},
{
name: 'reverse',
params: ['arrs'],
Expand Down Expand Up @@ -1382,6 +1412,8 @@ local html = import 'html.libsonnet';
description: html.paragraphs([
|||
Return the min of all element in <code>arr</code>.
|||,
]),
},
{
name: 'maxArray',
Expand All @@ -1390,6 +1422,8 @@ local html = import 'html.libsonnet';
description: html.paragraphs([
|||
Return the max of all element in <code>arr</code>.
|||,
]),
},
{
name: 'contains',
Expand All @@ -1401,6 +1435,16 @@ local html = import 'html.libsonnet';
|||,
]),
},
{
name: 'avg',
params: ['arr'],
availableSince: '0.20.0',
description: html.paragraphs([
|||
Return average of all element in <code>arr</code>.
|||,
]),
},
{
name: 'remove',
params: ['arr', 'elem'],
Expand Down
19 changes: 19 additions & 0 deletions stdlib/std.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -867,6 +867,12 @@ limitations under the License.
flattenArrays(arrs)::
std.foldl(function(a, b) a + b, arrs, []),

flattenDeepArray(value)::
if std.isArray(value) then
[y for x in value for y in std.flattenDeepArray(x)]
else
[value],

manifestIni(ini)::
local body_lines(body) =
std.join([], [
Expand Down Expand Up @@ -1705,6 +1711,12 @@ limitations under the License.

sum(arr):: std.foldl(function(a, b) a + b, arr, 0),

avg(arr)::
if std.length(arr) == 0 then
error 'Cannot calculate average of an empty array.'
else
std.sum(arr)/std.length(arr),

minArray(arr, keyF=id, onEmpty=error 'Expected at least one element in array. Got none')::
if std.length(arr) == 0 then
onEmpty
Expand Down Expand Up @@ -1740,6 +1752,11 @@ limitations under the License.
contains(arr, elem):: std.any([e == elem for e in arr]),

equalsIgnoreCase(str1, str2):: std.asciiLower(str1) == std.asciiLower(str2),

isEven(x):: std.round(x) % 2 == 0,
isOdd(x):: std.round(x) % 2 != 0,
isInteger(x):: std.round(x) == x,
isDecimal(x):: std.round(x) != x,

removeAt(arr, at):: [
arr[i],
Expand Down Expand Up @@ -1767,4 +1784,6 @@ limitations under the License.
sha512(str):: go_only_function,
sha3(str):: go_only_function,
parseXmlJsonml(str):: go_only_function,

trim(str):: std.stripChars(str, ' \t\n\f\r\u0085\u00A0'),
}
26 changes: 25 additions & 1 deletion test_suite/stdlib.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,11 @@ std.assertEqual(std.lines(['a', null, 'b']), 'a\nb\n') &&

std.assertEqual(std.flattenArrays([[1, 2, 3], [4, 5, 6], []]), [1, 2, 3, 4, 5, 6]) &&

std.assertEqual(std.flattenDeepArray([]), []) &&
std.assertEqual(std.flattenDeepArray([1, 2, 3]), [1, 2, 3]) &&
std.assertEqual(std.flattenDeepArray([1, [2, 3]]), [1, 2, 3]) &&
std.assertEqual(std.flattenDeepArray([[1], [2, 3], [[null]]]), [1, 2, 3, null]) &&

std.assertEqual(
std.manifestIni({
main: { a: '1', b: '2' },
Expand Down Expand Up @@ -1544,6 +1549,10 @@ std.assertEqual(std.all([]), true) &&

std.assertEqual(std.sum([1, 2, 3]), 6) &&

std.assertEqual(std.avg([1, 2, 3]), 2) &&
std.assertEqual(std.avg([0, 0, 0]), 0) &&
std.assertEqual(std.avg([1, 1, 2.5]), 1.5) &&

std.assertEqual(std.minArray([1, 2, 3]), 1) &&
std.assertEqual(std.minArray(['1', '2', '3']), '1') &&

Expand All @@ -1565,14 +1574,29 @@ std.assertEqual(std.isEmpty(''), true) &&
std.assertEqual(std.isEmpty('non-empty string'), false) &&

std.assertEqual(std.contains([1, 2, 3], 2), true) &&
std.assertEqual(std.contains([1, 2, 3], "foo"), false) &&
std.assertEqual(std.contains([1, 2, 3], 'foo'), false) &&

std.assertEqual(std.equalsIgnoreCase('foo', 'FOO'), true) &&
std.assertEqual(std.equalsIgnoreCase('foo', 'bar'), false) &&

std.assertEqual(std.isEven(10), true) &&
std.assertEqual(std.isEven(5), false) &&
std.assertEqual(std.isOdd(5), true) &&
std.assertEqual(std.isOdd(10), false) &&
std.assertEqual(std.isInteger(1), true) &&
std.assertEqual(std.isInteger(1.1), false) &&
std.assertEqual(std.isDecimal(1.1), true) &&
std.assertEqual(std.isDecimal(1), false) &&

std.assertEqual(std.remove([1, 2, 3], 2), [1, 3]) &&
std.assertEqual(std.removeAt([1, 2, 3], 1), [1, 3]) &&

std.assertEqual(std.objectRemoveKey({ foo: 1, bar: 2, baz: 3 }, 'foo'), { bar: 2, baz: 3 }) &&

std.assertEqual(std.trim('already trimmed string'), 'already trimmed string') &&
std.assertEqual(std.trim(' string with spaces on both ends '), 'string with spaces on both ends') &&
std.assertEqual(std.trim('string with newline character at end\n'), 'string with newline character at end') &&
std.assertEqual(std.trim('string with tabs at end\t\t'), 'string with tabs at end') &&
std.assertEqual(std.trim('string with other special whitespaces at end\f\r\u0085\u00A0'), 'string with carriage return at end') &&

true

0 comments on commit 0c3ff5b

Please sign in to comment.