Initial Dever 0.0.1
This commit is contained in:
commit
81233d098c
1479 files changed
+163782
No files matched your search
@@ -0,0 +1 @@
|
||||
long cmd run = app.run
|
||||
@@ -0,0 +1,56 @@
|
||||
type State {
|
||||
source: Json
|
||||
update: Bool
|
||||
checksum: Int
|
||||
}
|
||||
|
||||
run(source: Json, update: Bool, ticks: Text) (sample: Sample) {
|
||||
input = dever.bytes.fromText(ticks)
|
||||
initial = State {
|
||||
source = source
|
||||
update = update
|
||||
checksum = 0
|
||||
}
|
||||
output = transform(source, update)
|
||||
begin = dever.time.monotonicNanos()
|
||||
final = reduce(step, input, initial)
|
||||
end = dever.time.monotonicNanos()
|
||||
sample = Sample {
|
||||
elapsed_ns = end - begin
|
||||
checksum = final.checksum
|
||||
output = output
|
||||
}
|
||||
}
|
||||
|
||||
type Sample {
|
||||
elapsed_ns: Int
|
||||
checksum: Int
|
||||
output: Text
|
||||
}
|
||||
|
||||
step(unused: Int, state: State) (next: State) {
|
||||
output = transform(state.source, state.update)
|
||||
next = State {
|
||||
source = state.source
|
||||
update = state.update
|
||||
checksum = state.checksum + length(dever.bytes.fromText(output))
|
||||
}
|
||||
}
|
||||
|
||||
transform(source: Json, update: false) (output: Text) {
|
||||
output = selected(dever.json.propertyValue(source, "data"))
|
||||
}
|
||||
|
||||
transform(source: Json, update: true) (output: Text) {
|
||||
output = dever.json.toText(
|
||||
dever.json.setProperty(source, "active", dever.json.boolean(true))
|
||||
)
|
||||
}
|
||||
|
||||
selected(value: Json) (output: Text) {
|
||||
output = dever.json.toText(value)
|
||||
}
|
||||
|
||||
selected(value: null) (output: Text) {
|
||||
output = "null"
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
type input struct {
|
||||
Source json.RawMessage `json:"source"`
|
||||
Update bool `json:"update"`
|
||||
Ticks string `json:"ticks"`
|
||||
}
|
||||
|
||||
type sample struct {
|
||||
Elapsed int64 `json:"elapsed_ns"`
|
||||
Checksum int `json:"checksum"`
|
||||
Output string `json:"output"`
|
||||
}
|
||||
|
||||
func transform(source []byte, update bool) []byte {
|
||||
decoder := json.NewDecoder(bytes.NewReader(source))
|
||||
decoder.UseNumber()
|
||||
var object map[string]any
|
||||
if err := decoder.Decode(&object); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
var selected any = object["data"]
|
||||
if update {
|
||||
object["active"] = true
|
||||
selected = object
|
||||
}
|
||||
output, err := json.Marshal(selected)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return output
|
||||
}
|
||||
|
||||
func main() {
|
||||
var arguments input
|
||||
if len(os.Args) != 2 {
|
||||
panic("expected one JSON input")
|
||||
}
|
||||
if err := json.Unmarshal([]byte(os.Args[1]), &arguments); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if !json.Valid(arguments.Source) {
|
||||
panic("invalid source")
|
||||
}
|
||||
output := transform(arguments.Source, arguments.Update)
|
||||
checksum := 0
|
||||
begin := time.Now()
|
||||
for range []byte(arguments.Ticks) {
|
||||
checksum += len(transform(arguments.Source, arguments.Update))
|
||||
}
|
||||
result := sample{time.Since(begin).Nanoseconds(), checksum, string(output)}
|
||||
encoded, err := json.Marshal(result)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Println(string(encoded))
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::{Value, value::RawValue};
|
||||
use std::hint::black_box;
|
||||
use std::time::Instant;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct Input {
|
||||
source: Box<RawValue>,
|
||||
update: bool,
|
||||
ticks: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct Sample {
|
||||
elapsed_ns: u128,
|
||||
checksum: usize,
|
||||
output: String,
|
||||
}
|
||||
|
||||
fn transform(source: &str, update: bool) -> String {
|
||||
let mut value: Value = serde_json::from_str(source).expect("valid JSON input");
|
||||
let object = value.as_object_mut().expect("object input");
|
||||
if update {
|
||||
object.insert("active".into(), Value::Bool(true));
|
||||
serde_json::to_string(&value).expect("serializable object")
|
||||
} else {
|
||||
serde_json::to_string(object.get("data").expect("data field")).expect("serializable value")
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let argument = std::env::args().nth(1).expect("one JSON argument");
|
||||
let input: Input = serde_json::from_str(&argument).expect("valid input fields");
|
||||
let output = transform(input.source.get(), input.update);
|
||||
let mut checksum = 0;
|
||||
let started = Instant::now();
|
||||
for _ in input.ticks.bytes() {
|
||||
checksum += transform(black_box(input.source.get()), input.update).len();
|
||||
}
|
||||
let sample = Sample {
|
||||
elapsed_ns: started.elapsed().as_nanos(),
|
||||
checksum,
|
||||
output,
|
||||
};
|
||||
println!("{}", serde_json::to_string(&sample).unwrap());
|
||||
}
|
||||
Reference in new issue
Block a user