Skip to content

Commit

Permalink
feat: add generated jsonnet library
Browse files Browse the repository at this point in the history
  • Loading branch information
Duologic committed Jan 8, 2024
1 parent dfe360b commit ef2da18
Show file tree
Hide file tree
Showing 9 changed files with 2,032 additions and 0 deletions.
14 changes: 14 additions & 0 deletions jsonnet/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
ROOT_DIR:=$(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))

generator/schema.json:
cd ../ && yarn install && \
npx --yes ts-json-schema-generator -p ./src/datasource/types.ts --tsconfig ./tsconfig.json -t ZabbixMetricsQuery -o $(ROOT_DIR)/generator/schema.json

zabbix-query-libsonnet/main.libsonnet:
jsonnet -J generator/vendor -S $(ROOT_DIR)/generator/main.libsonnet \
| jsonnetfmt --no-use-implicit-plus - > $(ROOT_DIR)/zabbix-query-libsonnet/main.libsonnet

zabbix-query-libsonnet/docs/:
@rm -rf zabbix-query-libsonnet/docs/ && \
jsonnet -J generator/vendor -S -c -m zabbix-query-libsonnet/docs/ \
--exec "(import 'doc-util/main.libsonnet').render(import 'zabbix-query-libsonnet/main.libsonnet')"
15 changes: 15 additions & 0 deletions jsonnet/generator/jsonnetfile.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"version": 1,
"dependencies": [
{
"source": {
"git": {
"remote": "https://github.com/crdsonnet/crdsonnet.git",
"subdir": "crdsonnet"
}
},
"version": "master"
}
],
"legacyImports": true
}
56 changes: 56 additions & 0 deletions jsonnet/generator/jsonnetfile.lock.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{
"version": 1,
"dependencies": [
{
"source": {
"git": {
"remote": "https://github.com/Duologic/jsonnet-libsonnet.git",
"subdir": ""
}
},
"version": "bd08bf5f0ffc156bd755b916367bceb7fbe3de03",
"sum": "ZYc3KrgzQIciDlQ786i2iHeqEZqjnB8KlPiX6w8Mp38="
},
{
"source": {
"git": {
"remote": "https://github.com/crdsonnet/crdsonnet.git",
"subdir": "crdsonnet"
}
},
"version": "940cd3c5ae0c5ee4f24268fb486ae12d2a0c606a",
"sum": "dS+RF4Jig6dEfqq4sJ37XlefV66W1syOz7QMCPEzbZk="
},
{
"source": {
"git": {
"remote": "https://github.com/crdsonnet/validate-libsonnet.git",
"subdir": ""
}
},
"version": "a78ca15fbfece3110c4807d1f059132ece01d97b",
"sum": "qYLH56MqvmgxE4YMNeTbuJ1XSsCpl1sumHN5x8IQv2I="
},
{
"source": {
"git": {
"remote": "https://github.com/jsonnet-libs/docsonnet.git",
"subdir": "doc-util"
}
},
"version": "6ac6c69685b8c29c54515448eaca583da2d88150",
"sum": "BrAL/k23jq+xy9oA7TWIhUx07dsA/QLm3g7ktCwe//U="
},
{
"source": {
"git": {
"remote": "https://github.com/jsonnet-libs/xtd.git",
"subdir": ""
}
},
"version": "80bdea46b69cfbd5a6b57789ad856d3cb525e956",
"sum": "eUSd6nmI07Zl4pYuDnhasQ7Ua37HfHcBVItfvroVUGU="
}
],
"legacyImports": false
}
93 changes: 93 additions & 0 deletions jsonnet/generator/main.libsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
local schema = import './schema.json';

local j = import 'github.com/Duologic/jsonnet-libsonnet/main.libsonnet';
local jutils = import 'github.com/Duologic/jsonnet-libsonnet/utils.libsonnet';
local crdsonnet = import 'github.com/crdsonnet/crdsonnet/crdsonnet/main.libsonnet';
local astengine = import 'github.com/crdsonnet/crdsonnet/crdsonnet/renderEngines/ast.libsonnet';
local d = import 'github.com/jsonnet-libs/docsonnet/doc-util/main.libsonnet';

local processor =
crdsonnet.processor.new()
+ crdsonnet.processor.withRenderEngineType('ast');


local unwrapFromCRDsonnet(astObject, title) =
jutils.get(
astObject,
title,
default=error 'field %s not found in ast' % title
).expr;

local addDoc(obj) =
j.object.members(
[
j.field.field(
j.fieldname.string('#'),
// render docsonnet as literal to avoid docsonnet dependency
j.literal(
d.package.new(
'zabbixQuery',
'https://github.com/grafana/grafana-zabbix/jsonnet/zabbix-query-libsonnet',
'Jsonnet library for creating Zabbix queries for Grafana.',
'main.libsonnet',
'main',
),
),
nobreak=true,
),
]
+ std.filter(
// '#' docstring replaced by above
function(m) jutils.fieldnameValue(m.fieldname) != '#',
obj.members
)
);

local hideDocs(obj) =
// The sub fields of these objects can be accessed directly.
// For example: `application.withFilter`
local objFields = [
'application',
'datasource',
'group',
'host',
'item',
'itemTag',
'macro',
'options',
'proxy',
'tags',
'trigger',
'triggers',
];

local docsToHide =
std.map(
function(f) '#%s' % astengine.functionName(f),
objFields
)
+ std.map(
function(f) '#%sMixin' % astengine.functionName(f),
objFields
);

j.object.members(
std.filter(
function(m)
!std.member(
docsToHide,
jutils.fieldnameValue(m.fieldname)
),
obj.members,
)
);

local render = crdsonnet.schema.render('item', schema, processor);

local unwrapped = unwrapFromCRDsonnet(render, 'item');

local renderWithDoc = addDoc(unwrapped);

local renderWithHiddenDocs = hideDocs(renderWithDoc);

renderWithHiddenDocs.toString(break='\n')
Loading

0 comments on commit ef2da18

Please sign in to comment.