-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The rego init command was removed in a prior patch when we moved to go:embed. This patch brings back the feature and modernizes the initial specset.
- Loading branch information
1 parent
4e113e6
commit 3c01345
Showing
14 changed files
with
383 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
MAKEFLAGS += --warn-undefined-variables | ||
SHELL := /bin/bash -o pipefail | ||
|
||
default: format codegen | ||
|
||
.PHONY:codegen | ||
codegen: | ||
@ elegen folder -d specs -o codegen -g openapi3 || exit 1 | ||
@ rm -rf openapi3 | ||
@ elegen folder -d specs -o codegen || exit 1 | ||
@ mv custom_validations.go custom_validations.go.keep | ||
@ mv custom_validations_test.go custom_validations_test.go.keep | ||
@ rm -rf ./*.go | ||
@ mv custom_validations.go.keep custom_validations.go | ||
@ mv custom_validations_test.go.keep custom_validations_test.go | ||
@ mv codegen/elemental/*.go ./ | ||
@ mv codegen/openapi3 openapi3 | ||
@ rm -rf codegen doc | ||
@ mkdir -p doc | ||
@ data=$$(rego doc -d specs || exit 1) && echo -e "$${data}" > doc/documentation.md | ||
|
||
format: format-specs format-type format-validation format-parameter | ||
format-specs: | ||
@ for f in specs/*.abs; do \ | ||
rego format < $$f > $$f.formatted && \ | ||
mv $$f.formatted $$f; \ | ||
done | ||
@ for f in specs/*.spec; do \ | ||
rego format < $$f > $$f.formatted && \ | ||
mv $$f.formatted $$f; \ | ||
done | ||
|
||
format-type: target = "specs/_type.mapping" | ||
format-type: | ||
@ rego format -m typemapping < $(target) > $(target).formatted | ||
@ mv $(target).formatted $(target) | ||
|
||
format-validation: target = "specs/_validation.mapping" | ||
format-validation: | ||
@ rego format -m validationmapping < $(target) > $(target).formatted | ||
@ mv $(target).formatted $(target) | ||
|
||
format-parameter: target = "specs/_parameter.mapping" | ||
format-parameter: | ||
@ rego format -m parametermapping < $(target) > $(target).formatted | ||
@ mv $(target).formatted $(target) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package api | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"net/url" | ||
|
||
) | ||
|
||
// ValidateURL validates the given value is a correct url. | ||
func ValidateURL(attribute string, u string) error { | ||
|
||
uu, err := url.Parse(u) | ||
if err != nil { | ||
return makeErr(attribute, fmt.Sprintf("invalid url: %s", err)) | ||
} | ||
|
||
switch uu.Scheme { | ||
case "http", "https": | ||
case "": | ||
return makeErr(attribute, "invalid url: missing scheme") | ||
default: | ||
return makeErr(attribute, "invalid url: invalid scheme") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func makeErr(attribute string, message string) elemental.Error { | ||
|
||
err := elemental.NewError( | ||
"Validation Error", | ||
message, | ||
"a3s", | ||
http.StatusUnprocessableEntity, | ||
) | ||
|
||
if attribute != "" { | ||
err.Data = map[string]any{"attribute": attribute} | ||
} | ||
|
||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package api | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestValidateURL(t *testing.T) { | ||
type args struct { | ||
attribute string | ||
u string | ||
} | ||
tests := []struct { | ||
name string | ||
args func(t *testing.T) args | ||
|
||
wantErr bool | ||
inspectErr func(err error, t *testing.T) //use for more precise error evaluation after test | ||
}{ | ||
{ | ||
"valid url", | ||
func(t *testing.T) args { | ||
return args{ | ||
"attr", | ||
"https://toto.com", | ||
} | ||
}, | ||
false, | ||
nil, | ||
}, | ||
{ | ||
"invalid url", | ||
func(t *testing.T) args { | ||
return args{ | ||
"attr", | ||
"wesh", | ||
} | ||
}, | ||
true, | ||
nil, | ||
}, | ||
{ | ||
"invalid url 2", | ||
func(t *testing.T) args { | ||
return args{ | ||
"attr", | ||
"", | ||
} | ||
}, | ||
true, | ||
nil, | ||
}, | ||
{ | ||
"invalid url 3", | ||
func(t *testing.T) args { | ||
return args{ | ||
"attr", | ||
"http##dd%", | ||
} | ||
}, | ||
true, | ||
nil, | ||
}, | ||
{ | ||
"invalid scheme", | ||
func(t *testing.T) args { | ||
return args{ | ||
"attr", | ||
"ftp://what.com", | ||
} | ||
}, | ||
true, | ||
nil, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
tArgs := tt.args(t) | ||
|
||
err := ValidateURL(tArgs.attribute, tArgs.u) | ||
|
||
if (err != nil) != tt.wantErr { | ||
t.Fatalf("ValidateURL error = %v, wantErr: %t", err, tt.wantErr) | ||
} | ||
|
||
if tt.inspectErr != nil { | ||
tt.inspectErr(err, t) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#!/bin/bash | ||
cd .. || exit 0 | ||
make codegen |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Attributes | ||
attributes: | ||
v1: | ||
- name: ID | ||
description: ID is the identifier of the object. | ||
type: string | ||
exposed: true | ||
stored: true | ||
read_only: true | ||
autogenerated: true | ||
filterable: true | ||
identifier: true | ||
orderable: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
prefix: api | ||
root: root | ||
version: 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
$queryable: | ||
entries: | ||
- name: q | ||
description: This is an example. | ||
type: string | ||
example_value: hello == world |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
'[][]string': | ||
elemental: | ||
type: '[][]string' | ||
init: '[][]string{}' | ||
jsonschema: | ||
type: |- | ||
{ | ||
"type": "array", | ||
"items": { | ||
"type": "array", | ||
"items": { | ||
"type": "string" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
$url: | ||
elemental: | ||
name: ValidateURL |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Model | ||
model: | ||
rest_name: object | ||
resource_name: objects | ||
entity_name: Objects | ||
package: default | ||
group: core | ||
description: This is random object. | ||
get: | ||
description: Gets the object. | ||
update: | ||
description: Updates the object. | ||
delete: | ||
description: Deletes the object. | ||
extends: | ||
- '@identifiable' | ||
|
||
# Attributes | ||
attributes: | ||
v1: | ||
- name: name | ||
description: The name of the object. | ||
type: string | ||
exposed: true | ||
stored: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[regolithe] | ||
product_name = api | ||
copyright = yourcompany | ||
|
||
[transformer] | ||
name = api | ||
url = www.mycompany.net/myapi | ||
author = you | ||
email = [email protected] | ||
version = 1.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Model | ||
model: | ||
rest_name: root | ||
resource_name: root | ||
entity_name: Root | ||
package: root | ||
group: core | ||
description: root object. | ||
get: | ||
description: gets the object. | ||
root: true | ||
|
||
# Relations | ||
relations: | ||
- rest_name: object | ||
get: | ||
description: Retrieves the list of objects. | ||
create: | ||
description: Creates a new object. |
Oops, something went wrong.