Skip to content

Commit

Permalink
feat: jsonnet first steps
Browse files Browse the repository at this point in the history
  • Loading branch information
vitorsalgado committed Aug 16, 2023
1 parent a278044 commit 5cd33f6
Show file tree
Hide file tree
Showing 17 changed files with 471 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ indent_size = 4
[{Makefile,go.mod,*.go}]
indent_style = tab

[{*.proto,*.json,*.yaml,*yml,*.tmpl,*.toml}]
[{*.proto,*.json,*.yaml,*yml,*.tmpl,*.toml,*.jsonnet,*.libsonnet}]
indent_size = 2

[*.md]
Expand Down
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ PROJECT_NAME=dz
DOCKER_IMAGE=$(PROJECT_NAME)
PROTO_MESSAGES = dzgrpc/internal/protobuf

JSONNET_FMT := jsonnetfmt -n 2 --max-blank-lines 2 --string-style s --comment-style s

# allow user specific optional overrides
-include Makefile.overrides

Expand Down Expand Up @@ -89,6 +91,10 @@ docs: ## show documentation website locally
@echo navigate to: http://localhost:6060/pkg/github.com/vitorsalgado/mocha/v3
@godoc -http=:6060

fmtj:
@find . -name '*.libsonnet' -print -o -name '*.jsonnet' -print | \
xargs -n 1 -- $(JSONNET_FMT) -i

.PHONY: tools
tools: ## install dev tools
@echo "tools"
Expand Down
131 changes: 131 additions & 0 deletions dzhttpjsonschema/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
{
"$id": "",
"title": "Mock Definition",
"description": "Describes an HTTP mock definition.",
"type": "object",
"properties": {
"id": { "type": "string" },
"name": { "type": "string" },
"priority": { "type": "integer" },
"enabled": { "type": "boolean" },
"delay": { "oneOf": [ { "type": "string" }, { "type": "number" } ] },
"repeat": { "type": "integer" },
"scenario": {
"type": "object",
"properties": {
"name": { "type": "string" },
"required_state": { "type": "string" },
"new_state": { "type": "string" }
},
"additionalProperties": false
},
"post_actions": { "type": "array", "item": { "$ref": "#/$defs/post_action" } },
"request": {
"type": "object",
"properties": {
"scheme": { "$ref": "#/$defs/matcher" },
"method": { "$ref": "#/$defs/matcher" },
"path": { "$ref": "#/$defs/matcher" },
"url": { "$ref": "#/$defs/matcher" },
"url_match": { "$ref": "#/$defs/matcher" },
"query": { "$ref": "#/$defs/keyed_matcher" },
"queries": { "$ref": "#/$defs/keyed_matcher" },
"header": { "$ref": "#/$defs/keyed_matcher" },
"form_url_encoded": { "$ref": "#/$defs/keyed_matcher" },
"body": { "$ref": "#/$defs/matcher" }
},
"additionalProperties": false
},
"response": { "$ref": "#/$defs/reply" },
"response_sequence": {
"type": "object",
"properties": {
"responses": { "type": "array", "items": { "$ref": "#/$defs/reply" } },
"after_ended": { "$ref": "#/$defs/reply" }
},
"required": [ "responses" ],
"additionalProperties": false
},
"response_random": {
"type": "object",
"properties": {
"responses": { "type": "array", "items": { "$ref": "#/$defs/reply" } },
"seed": { "type": "integer" }
},
"required": [ "responses" ],
"additionalProperties": false
},
"proxy": {
"type": "object",
"properties": {
"target": { "type": "string" },
"forward_header": { "$ref": "#/$defs/header_map" },
"header": { "$ref": "#/$defs/header_map" },
"remove_headers": { "type": "array", "items": { "type": "string" } },
"trim_prefix": { "type": "string" },
"trim_suffix": { "type": "string" },
"timeout": { "oneOf": [ { "type": "string" }, { "type": "number" } ] },
"ssl_verify": { "type": "boolean" },
"no_follow": { "type": "boolean" }
},
"required": [ "target" ],
"additionalProperties": false
},
"ext": { "type": "object" }
},
"required": [ "request" ],
"additionalProperties": false,
"$defs": {
"matcher": {
"oneOf": [
{ "type": "string" },
{ "type": "array", "minItems": 1 }
]
},
"keyed_matcher": {
"type": "object",
"patternProperties": {
"^.*$": { "$ref": "#/$defs/matcher" }
}
},
"reply": {
"type": "object",
"properties": {
"status": { "type": "integer" },
"header": { "$ref": "#/$defs/header_map" },
"header_template": { "$ref": "#/$defs/header_map" },
"body": { },
"body_file": { "type": "string" },
"template": {
"type": "object",
"properties": {
"enabled": { "type": "boolean" },
"data": { "oneOf": [ { "type": "object" }, { "type": "array" } ] }
}
},
"encoding": { "type": "string" }
}
},
"post_action": {
"type": "object",
"properties": {
"name": { "type": "string" },
"parameters": { }
},
"additionalProperties": false,
"required": [ "name" ]
},
"string_map": {
"type": "object",
"patternProperties": {
"^.*$": { "type": "string" }
}
},
"header_map": {
"type": "object",
"patternProperties": {
"^.*$": { "oneOf": [ {"type": "string"}, {"type": "array", "items": {"type": "string"} } ] }
}
}
}
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/go-jsonnet v0.20.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jwalton/go-supportscolor v1.1.0 // indirect
Expand All @@ -45,5 +46,7 @@ require (
google.golang.org/genproto v0.0.0-20230110181048-76db0878b65f // indirect
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.2.7 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
sigs.k8s.io/yaml v1.1.0 // indirect
)
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-jsonnet v0.20.0 h1:WG4TTSARuV7bSm4PMB4ohjxe33IHT5WVTrJSU33uT4g=
github.com/google/go-jsonnet v0.20.0/go.mod h1:VbgWF9JX7ztlv770x/TolZNGGFfiHEVx9G6ca2eUmeA=
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
github.com/google/martian/v3 v3.0.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0=
github.com/google/martian/v3 v3.1.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0=
Expand Down Expand Up @@ -526,6 +528,8 @@ gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.7 h1:VUgggvou5XRW9mHwD/yXxIYSMtY0zoKQf/v226p2nyo=
gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand All @@ -539,3 +543,5 @@ honnef.co/go/tools v0.0.1-2020.1.4/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
sigs.k8s.io/yaml v1.1.0 h1:4A07+ZFc2wgJwo8YNlQpr1rVlgUDlxXHhPJciaPY5gs=
sigs.k8s.io/yaml v1.1.0/go.mod h1:UJmg0vDUVViEyp3mgSv9WPwZCDxu4rQW1olrI1uml+o=
74 changes: 74 additions & 0 deletions jsonnet/http.libsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
local header = import './http_header.libsonnet';
local mediaTypes = import './http_media_types.libsonnet';
local method = import './http_method.libsonnet';
local statuses = import './http_status.libsonnet';
local dz = import './internal/internal.libsonnet';
local scheme = import './scheme.libsonnet';

(scheme) +
(method) +
(header) +
(mediaTypes) +
(statuses) +

{
mock(v):: dz.merge(v),

id(id):: { id: id },
name(n):: { name: n },
priority(p):: { priority: p },
enabled(e=true):: { enabled: e },

delay(duration):: { delay: duration },
repeat(r):: { [if !std.isNumber(r) then error 'repeat must be a number' else 'repeat']: r },
scenario(name, requiredState='', newState=''):: { scenario: { name: name, [if std.isString(requiredState) && !std.isEmpty(requiredState) then 'required_state']: requiredState, [if std.isString(newState) && !std.isEmpty(newState) then 'new_state']: newState } },

request(fields):: { request: dz.merge(fields) },
req: {
scheme(matcher):: { scheme: matcher },
isHTTP():: self.scheme(scheme.SchemeHTTP),
isHTTPS():: self.scheme(scheme.SchemeHTTPS),

method(v):: { method: if std.isString(v) then std.asciiUpper(v) else v },
get():: self.method(method.MethodGet),
head():: self.method(method.MethodHead),
post():: self.method(method.MethodPost),
put():: self.method(method.MethodPut),
patch():: self.method(method.MethodPatch),
delete():: self.method(method.MethodDelete),
options():: self.method(method.MethodOptions),
trace():: self.method(method.MethodTrace),

path(matcher):: { path: matcher },
url(matcher):: { url: matcher },
urlMatch(matcher):: { url_match: matcher },
header(key, matcher):: { header: { [key]: matcher } },
query(key, matcher):: { query: { [key]: matcher } },
queries(key, matcher):: { queries: { [key]: matcher } },
formURLEncoded(key, matcher):: { form_url_encoded: { [key]: matcher } },
body(matcher):: { body: matcher },
},

response(v):: { response: dz.merge(v) },
res: {
status(code):: { status: code },
ok():: self.status(statuses.StatusOK),

header(map):: { header: map },
headerTemplate(map):: { header: map },
body(b):: { body: b },
bodyFile(f):: { body_file: f },
encoding(e):: { encoding: e },
gzip():: self.encoding('gzip'),

sequence(list):: { response_sequence: dz.merge(list) },
seq: {
afterEnded(reply): { after_ended: reply },
},
},

postAction(name, parameters=null):: { name: name, [if std.type(parameters) != 'null' then 'parameters']: parameters },
postActions(actions):: { [if !std.isArray(actions) then error 'postActions expects an array of post actions' else 'post_actions']: actions },

ext(v):: { ext: v },
}
50 changes: 50 additions & 0 deletions jsonnet/http_header.libsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
HeaderAccept: 'Accept',
HeaderAcceptEncoding: 'Accept-Encoding',
HeaderContentType: 'Content-Type',
HeaderContentEncoding: 'Content-Encoding',
HeaderAllow: 'Allow',
HeaderAuthorization: 'Authorization',
HeaderContentDisposition: 'Content-Disposition',
HeaderVary: 'Vary',
HeaderOrigin: 'Origin',
HeaderContentLength: 'Content-length',
HeaderConnection: 'Connection',
HeaderTrailer: 'Trailer',
HeaderLocation: 'Location',
HeaderCacheControl: 'Cache-Control',
HeaderCookie: 'Cookie',
HeaderSetCookie: 'Set-Cookie',
HeaderIfModifiedSince: 'If-Modified-Since',
HeaderLastModified: 'Last-Modified',
HeaderRetryAfter: 'Retry-After',
HeaderUpgrade: 'Upgrade',
HeaderWWWAuthenticate: 'WWW-Authenticate',
HeaderXForwardedFor: 'X-Forwarded-For',
HeaderXForwardedProto: 'X-Forwarded-Proto',
HeaderXForwardedProtocol: 'X-Forwarded-Protocol',
HeaderXForwardedSsl: 'X-Forwarded-Ssl',
HeaderXUrlScheme: 'X-Url-Scheme',
HeaderXHTTPMethodOverride: 'X-HTTP-Method-Override',
HeaderXRealIP: 'X-Real-Ip',
HeaderXRequestID: 'X-Request-Id',
HeaderXCorrelationID: 'X-Correlation-Id',
HeaderXRequestedWith: 'X-Requested-With',
HeaderServer: 'Server',
HeaderAccessControlRequestMethod: 'Access-Control-Request-Method',
HeaderAccessControlAllowOrigin: 'Access-Control-Allow-Origin',
HeaderAccessControlAllowMethods: 'Access-Control-Allow-Methods',
HeaderAccessControlAllowHeaders: 'Access-Control-Allow-Header',
HeaderAccessControlExposeHeaders: 'Access-Control-Expose-Header',
HeaderAccessControlMaxAge: 'Access-Control-max-Age',
HeaderAccessControlAllowCredentials: 'Access-Control-Allow-Credentials',
HeaderAccessControlRequestHeaders: 'Access-Control-Request-Header',
HeaderStrictTransportSecurity: 'Strict-Transport-Security',
HeaderXContentTypeOptions: 'X-Content-Type-Options',
HeaderXXSSProtection: 'X-XSS-Protection',
HeaderXFrameOptions: 'X-Frame-Options',
HeaderContentSecurityPolicy: 'Content-Security-Policy',
HeaderContentSecurityPolicyReportOnly: 'Content-Security-Policy-Report-Only',
HeaderXCSRFToken: 'X-CSRF-Token',
HeaderReferrerPolicy: 'Referrer-Policy',
}
21 changes: 21 additions & 0 deletions jsonnet/http_media_types.libsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
local _charsetUTF8 = 'charset:UTF-8';

{
MIMEApplicationJSON: 'application/json',
MIMEApplicationCharsetUTF8: self.MIMEApplicationJSON + '; ' + _charsetUTF8,
MIMETextPlain: 'text/plain',
MIMETextPlainCharsetUTF8: self.MIMETextPlain + '; ' + _charsetUTF8,
MIMETextHTML: 'text/html',
MIMETextHTMLCharsetUTF8: self.MIMETextHTML + '; ' + _charsetUTF8,
MIMETextXML: 'text/xml',
MIMETextXMLCharsetUTF8: self.MIMETextXML + '; ' + _charsetUTF8,
MIMEFormURLEncoded: 'application/x-www-form-urlencoded',
MIMEFormURLEncodedCharsetUTF8: self.MIMEFormURLEncoded + '; ' + _charsetUTF8,
MIMEApplicationJavaScript: 'application/javascript',
MIMEApplicationJavaScriptCharsetUTF8: self.MIMEApplicationJavaScript + '; ' + _charsetUTF8,
MIMEApplicationXML: 'application/xml',
MIMEApplicationXMLCharsetUTF8: self.MIMEApplicationXML + '; ' + _charsetUTF8,
MIMEApplicationProtobuf: 'application/protobuf',
MIMEMultipartForm: 'multipart/form-data',
MIMEOctetStream: 'application/octet-stream',
}
10 changes: 10 additions & 0 deletions jsonnet/http_method.libsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
MethodGet: 'GET',
MethodHead: 'HEAD',
MethodPost: 'POST',
MethodPut: 'PUT',
MethodPatch: 'PATCH',
MethodDelete: 'DELETE',
MethodOptions: 'OPTIONS',
MethodTrace: 'TRACE',
}
Loading

0 comments on commit 5cd33f6

Please sign in to comment.