-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Continue Updating Claro Docs to Use Auto-Validated Examples
- Loading branch information
1 parent
4d9f527
commit 02617cf
Showing
9 changed files
with
95 additions
and
5 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
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,25 @@ | ||
load("//:rules.bzl", "claro_module", "claro_binary") | ||
load("//mdbook_docs:docs_with_validated_examples.bzl", "doc_with_validated_examples") | ||
|
||
doc_with_validated_examples( | ||
name = "json_parsing", | ||
doc_template = "json_parsing.tmpl.md", | ||
examples = [ | ||
{ | ||
"example": "//mdbook_docs/src/module_system/module_apis/static_values:ex-server-config.json", | ||
"executable": False, | ||
"codeblock_css_class": "json", | ||
}, | ||
{ | ||
"example": "ex1-format.claro", | ||
"append_output": False, | ||
}, | ||
{ | ||
"example": "ex1.claro", | ||
"hidden_setup": "ex1-format.claro", | ||
"resources": { | ||
"ServerConfigJSON": "//mdbook_docs/src/module_system/module_apis/static_values:ex-server-config.json" | ||
}, | ||
}, | ||
], | ||
) |
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,12 @@ | ||
alias ServerConfig : struct { | ||
server_name: string, | ||
port: int, | ||
logging: struct { | ||
filename: string | ||
}, | ||
database: struct { | ||
host: string, | ||
port: int, | ||
database_name: 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,15 @@ | ||
|
||
resources::ServerConfigJSON | ||
|> files::readOrPanic(^) | ||
|> var parsedConfig: std::ParsedJson<ServerConfig> = fromJson(^); | ||
|
||
var parsedResult = unwrap(parsedConfig).result; | ||
if (parsedResult instanceof ServerConfig) { | ||
print("Config has correct format and was parsed successfully!"); | ||
print(strings::repeated("-", 50)); | ||
print("Server Name: {parsedResult.server_name}"); | ||
print("Port: {parsedResult.port}"); | ||
} else { | ||
print("JSON parsing failed!"); | ||
print(parsedResult); | ||
} |
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,34 @@ | ||
# JSON Parsing | ||
|
||
Claro strives to make development easier in many ways, and as being able to write programs that interact with the | ||
network is an important goal, Claro has some initial support for automatically generating efficient JSON parsers for | ||
relatively arbitrary formats. If you know the schema of the JSON data that you'll be interacting with, and can describe | ||
it as some Claro struct, then in general you can automatically parse JSON data from a string directly into the Claro | ||
type. | ||
|
||
Claro's JSON parsing is implemented by **generating a custom parser for the target data format at compile time**. So, in | ||
addition to ergonomic improvements, this approach offers potential performance benefits over a general-purpose JSON | ||
parser. | ||
|
||
For example, the following JSON string could be included in a | ||
[Resource File](../resource_files/resource_files.generated_docs.md): | ||
|
||
{{EX1}} | ||
|
||
We can represent that JSON format as the following Claro data structure: | ||
|
||
{{EX2}} | ||
|
||
And now, the JSON string can be parsed by a simple call to the `fromJson(...)` builtin function: | ||
|
||
{{EX3}} | ||
|
||
## Limitations | ||
|
||
<div class="warning"> | ||
|
||
To be clear, Claro's JSON parsing support is currently fairly constrained and doesn't yet support the full range of | ||
possible JSON formats. You'll be warned at compile-time if the format you're attempting to auto-parse is supported or | ||
not. More work will be needed to complete the implementation. **If you're interested in contributing to this please | ||
reach out!** | ||
</div> |
2 changes: 2 additions & 0 deletions
2
mdbook_docs/src/module_system/module_apis/static_values/BUILD
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