2017-10-14 02:48:54 +02:00
|
|
|
// Copyright 2016-2017, Pulumi Corporation. All rights reserved.
|
|
|
|
|
|
|
|
import * as pulumi from "pulumi";
|
2017-10-14 23:29:49 +02:00
|
|
|
import * as dynamic from "pulumi/dynamic";
|
2017-10-14 02:48:54 +02:00
|
|
|
|
2017-10-14 23:29:49 +02:00
|
|
|
class OperatorCallbacks implements dynamic.ResourceProvider {
|
2017-10-14 02:48:54 +02:00
|
|
|
private op: (l: number, r: number) => any;
|
|
|
|
|
|
|
|
constructor(op: (l: number, r: number) => any) {
|
|
|
|
this.op = op;
|
|
|
|
}
|
|
|
|
|
2017-10-14 23:29:49 +02:00
|
|
|
check = (inputs: any) => Promise.resolve(new dynamic.CheckResult(undefined, []));
|
|
|
|
diff = (id: pulumi.ID, olds: any, news: any) => Promise.resolve(new dynamic.DiffResult([], []));
|
2017-10-14 02:48:54 +02:00
|
|
|
delete = (id: pulumi.ID, props: any) => Promise.resolve();
|
|
|
|
|
2017-10-14 23:29:49 +02:00
|
|
|
create = (inputs: any) => Promise.resolve(new dynamic.CreateResult("0", this.op(Number(inputs.left), Number(inputs.right))));
|
2017-10-14 02:48:54 +02:00
|
|
|
|
2017-10-14 23:29:49 +02:00
|
|
|
update = (id: string, olds: any, news: any) => Promise.resolve(new dynamic.UpdateResult(this.op(Number(news.left), Number(news.right))));
|
2017-10-14 02:48:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class DivCallbacks extends OperatorCallbacks {
|
|
|
|
constructor() {
|
|
|
|
super((left: number, right: number) => <any>{ quotient: Math.floor(left / right), remainder: left % right });
|
|
|
|
}
|
|
|
|
|
2017-10-14 23:29:49 +02:00
|
|
|
check = (ins: any) => Promise.resolve(new dynamic.CheckResult(undefined, ins.right == 0 ? [ new dynamic.CheckFailure("right", "divisor must be non-zero") ] : []));
|
2017-10-14 02:48:54 +02:00
|
|
|
}
|
|
|
|
|
2017-10-14 23:29:49 +02:00
|
|
|
class Add extends dynamic.Resource {
|
2017-10-14 02:48:54 +02:00
|
|
|
public readonly sum: pulumi.Computed<number>;
|
|
|
|
|
|
|
|
private static callbacks = new OperatorCallbacks((left: number, right: number) => <any>{ sum: left + right });
|
|
|
|
|
|
|
|
constructor(name: string, left: pulumi.ComputedValue<number>, right: pulumi.ComputedValue<number>) {
|
|
|
|
super(Add.callbacks, name, {left: left, right: right, sum: undefined}, undefined);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-14 23:29:49 +02:00
|
|
|
class Mul extends dynamic.Resource {
|
2017-10-14 02:48:54 +02:00
|
|
|
public readonly product: pulumi.Computed<number>;
|
|
|
|
|
|
|
|
private static callbacks = new OperatorCallbacks((left: number, right: number) => <any>{ product: left * right });
|
|
|
|
|
|
|
|
constructor(name: string, left: pulumi.ComputedValue<number>, right: pulumi.ComputedValue<number>) {
|
|
|
|
super(Mul.callbacks, name, {left: left, right: right, product: undefined}, undefined);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-14 23:29:49 +02:00
|
|
|
class Sub extends dynamic.Resource {
|
2017-10-14 02:48:54 +02:00
|
|
|
public readonly difference: pulumi.Computed<number>;
|
|
|
|
|
|
|
|
private static callbacks = new OperatorCallbacks((left: number, right: number) => <any>{ difference: left - right });
|
|
|
|
|
|
|
|
constructor(name: string, left: pulumi.ComputedValue<number>, right: pulumi.ComputedValue<number>) {
|
|
|
|
super(Sub.callbacks, name, {left: left, right: right, difference: undefined}, undefined);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-14 23:29:49 +02:00
|
|
|
class Div extends dynamic.Resource {
|
2017-10-14 02:48:54 +02:00
|
|
|
public readonly quotient: pulumi.Computed<number>;
|
|
|
|
public readonly remainder: pulumi.Computed<number>;
|
|
|
|
|
|
|
|
private static callbacks = new DivCallbacks();
|
|
|
|
|
|
|
|
constructor(name: string, left: pulumi.ComputedValue<number>, right: pulumi.ComputedValue<number>) {
|
|
|
|
super(Div.callbacks, name, {left: left, right: right, quotient: undefined, remainder: undefined}, undefined);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let run = async () => {
|
|
|
|
let config = new pulumi.Config("simple:config");
|
|
|
|
|
|
|
|
let w = Number(config.require("w")), x = Number(config.require("x")), y = Number(config.require("y"));
|
|
|
|
|
|
|
|
let sum = new Add("sum", x, y);
|
|
|
|
let square = new Mul("square", sum.sum, sum.sum);
|
|
|
|
let diff = new Sub("diff", square.product, w);
|
|
|
|
let divrem = new Div("divrem", diff.difference, sum.sum);
|
|
|
|
let result = new Add("result", divrem.quotient, divrem.remainder);
|
|
|
|
|
|
|
|
console.log(`((x + y)^2 - w) / (x + y) + ((x + y)^2 - w) %% (x + y) = ${await result.sum}`);
|
|
|
|
};
|
|
|
|
|
|
|
|
run();
|