Skip to content
This repository has been archived by the owner on Sep 1, 2023. It is now read-only.

Commit

Permalink
1.0.9: Added (undocumented) support for 'file' input type
Browse files Browse the repository at this point in the history
- Added a new expectedInput type, 'file'

- Enables devs to specify that (API and UI) clients should supply a file
  input for the job. The UI renders this as an <input type="file" />

- Using a file input in a template expression (e.g. '${inputs.someFile}')
  automatically coerces the user-supplied file into a local filepath that
  your application can read.

  - This is effectively sugar for '${toFile(inputs.foo)}', but with some
    extra quality-of-life benefits.

  - The main QOL benefit is that Jobson will try to leave the user-supplied
    filename untouched, which is useful if your application relies on
    extension matching

- This feature is currently undocumented due to time constraints. The final
  API of the released version *should* (famous last words) be identical.
  • Loading branch information
adamkewley committed Jul 1, 2020
2 parents 6859b1c + 60e1143 commit d191550
Show file tree
Hide file tree
Showing 30 changed files with 614 additions and 42 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ Requires java (8+):
```bash
# install and add to PATH
wget https://github.com/adamkewley/jobson/releases/download/1.0.8/jobson-nix-1.0.8.tar.gz
tar xvf jobson-nix-1.0.8.tar.gz
export PATH=$PATH:jobson-nix-1.0.8/bin
wget https://github.com/adamkewley/jobson/releases/download/1.0.9/jobson-nix-1.0.9.tar.gz
tar xvf jobson-nix-1.0.9.tar.gz
export PATH=$PATH:jobson-nix-1.0.9/bin

# create demo workspace
jobson new --demo
Expand Down
4 changes: 2 additions & 2 deletions jobson-deb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>com.github.jobson</groupId>
<artifactId>jobson-project</artifactId>
<version>1.0.8</version>
<version>1.0.9</version>
</parent>

<artifactId>jobson-deb</artifactId>
Expand All @@ -17,7 +17,7 @@
<dependency>
<groupId>com.github.jobson</groupId>
<artifactId>jobson-nix</artifactId>
<version>1.0.8</version>
<version>1.0.9</version>
<type>tar.gz</type>
</dependency>
</dependencies>
Expand Down
4 changes: 2 additions & 2 deletions jobson-docker/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>com.github.jobson</groupId>
<artifactId>jobson-project</artifactId>
<version>1.0.8</version>
<version>1.0.9</version>
</parent>

<artifactId>jobson-docker</artifactId>
Expand All @@ -17,7 +17,7 @@
<dependency>
<groupId>com.github.jobson</groupId>
<artifactId>jobson-deb</artifactId>
<version>1.0.8</version>
<version>1.0.9</version>
<type>deb</type>
</dependency>
</dependencies>
Expand Down
4 changes: 2 additions & 2 deletions jobson-docs/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
<parent>
<groupId>com.github.jobson</groupId>
<artifactId>jobson-project</artifactId>
<version>1.0.8</version>
<version>1.0.9</version>
</parent>

<artifactId>jobson-docs</artifactId>
<version>1.0.8</version>
<version>1.0.9</version>
<packaging>pom</packaging>

<dependencies>
Expand Down
8 changes: 4 additions & 4 deletions jobson-nix/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>com.github.jobson</groupId>
<artifactId>jobson-project</artifactId>
<version>1.0.8</version>
<version>1.0.9</version>
</parent>

<artifactId>jobson-nix</artifactId>
Expand All @@ -17,18 +17,18 @@
<dependency>
<groupId>com.github.jobson</groupId>
<artifactId>jobson</artifactId>
<version>1.0.8</version>
<version>1.0.9</version>
</dependency>
<dependency>
<groupId>com.github.jobson</groupId>
<artifactId>jobson-docs</artifactId>
<version>1.0.8</version>
<version>1.0.9</version>
<type>tar.gz</type>
</dependency>
<dependency>
<groupId>com.github.jobson</groupId>
<artifactId>jobson-ui</artifactId>
<version>1.0.8</version>
<version>1.0.9</version>
<type>tar.gz</type>
</dependency>
</dependencies>
Expand Down
4 changes: 2 additions & 2 deletions jobson-swagger-ui/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
<parent>
<groupId>com.github.jobson</groupId>
<artifactId>jobson-project</artifactId>
<version>1.0.8</version>
<version>1.0.9</version>
</parent>

<artifactId>jobson-swagger-ui</artifactId>
<version>1.0.8</version>
<version>1.0.9</version>
<packaging>pom</packaging>

<dependencies>
Expand Down
6 changes: 3 additions & 3 deletions jobson-swagger/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@
<parent>
<groupId>com.github.jobson</groupId>
<artifactId>jobson-project</artifactId>
<version>1.0.8</version>
<version>1.0.9</version>
</parent>

<artifactId>jobson-swagger</artifactId>
<version>1.0.8</version>
<version>1.0.9</version>
<packaging>pom</packaging>

<dependencies>
<dependency>
<groupId>com.github.jobson</groupId>
<artifactId>jobson</artifactId>
<version>1.0.8</version>
<version>1.0.9</version>
<exclusions>
<exclusion>
<groupId>com.fasterxml</groupId>
Expand Down
4 changes: 2 additions & 2 deletions jobson-ui/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
<parent>
<groupId>com.github.jobson</groupId>
<artifactId>jobson-project</artifactId>
<version>1.0.8</version>
<version>1.0.9</version>
</parent>

<artifactId>jobson-ui</artifactId>
<version>1.0.8</version>
<version>1.0.9</version>
<packaging>pom</packaging>

<build>
Expand Down
127 changes: 127 additions & 0 deletions jobson-ui/src/ts/components/inputeditors/FileInputEditor.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import {Component, ReactElement} from "react";
import {InputEditorProps} from "./InputEditor";
import * as React from "react";
import {InputEditorUpdate} from "./updates/InputEditorUpdate";

const enum States {
Showing,
Loading,
}

interface ShowingState {
type: States.Showing;
error: string | null;
}

interface LoadingState {
type: States.Loading;
}

type State = ShowingState | LoadingState;

function toB64(file: File): Promise<string> {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = e => {
const contents = e.target.result as ArrayBuffer;
const b64 = btoa(String.fromCharCode.apply(null, new Uint8Array(contents)));
resolve(b64);
};
reader.onerror = e => {
reject(e);
};
reader.onabort = e => {
reject(e);
};
reader.readAsArrayBuffer(file);
});
}

export class FileInputEditor extends Component<InputEditorProps, State> {

public state: State = {
type: States.Showing,
error: null,
};

private fileInput: React.RefObject<HTMLInputElement> = React.createRef();

public render(): ReactElement<any> {
switch (this.state.type) {
case States.Showing:
return this.renderShowingState();
case States.Loading:
return this.renderLoadingState();
}
}

private renderShowingState(): ReactElement<any> {
return (
<div>
<input type="file"
ref={this.fileInput}
onChange={e => this.onFileInputChanged(e)}
required />
</div>
);
}

private renderLoadingState(): ReactElement<any> {
return (
<div>
<input type="file"
ref={this.fileInput}
onChange={e => this.onFileInputChanged(e)}
required />
Loading...
</div>
);
}

private onFileInputChanged(e: React.FormEvent): void {
const file: File = this.fileInput.current.files[0];
const filename = file.name;

this.setState({
type: States.Loading,
}, () => {
toB64(file)
.then(b64Str => {
const upd = InputEditorUpdate.value({
filename: file.name,
data: b64Str,
});
this.props.onJobInputUpdate(upd);
this.setState({
type: States.Showing,
error: null,
});
})
.catch(err => {
this.setState({
type: States.Showing,
error: err,
});
})
});
}
}
9 changes: 7 additions & 2 deletions jobson-ui/src/ts/components/inputeditors/InputEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {UnknownInputTypeInputEditor} from "./UnknownInputTypeInputEditor";
import {IntegerInputEditor} from "./IntegerInputEditor";
import {Constants} from "../../Constants";
import {DecimalInputEditor} from "./DecimalInputEditor";
import {FileInputEditor} from "./FileInputEditor";
import {APIExpectedInput} from "../../apitypes/APIExpectedInput";
import {InputEditorUpdate} from "./updates/InputEditorUpdate";
import {Component, ReactElement} from "react";
Expand Down Expand Up @@ -75,15 +76,19 @@ export class InputEditor extends Component<InputEditorProps> {
max: Constants.F64_MAX,
typeName: "double"
}, props),
"file": props => new FileInputEditor(props),
};

private static unknownInputCtor = props => new UnknownInputTypeInputEditor(props);

public static getSupportedInputEditors(): string[] {
return Object.keys(this.expectedInputUiComponentCtors);
}

public render(): ReactElement<any> {
const unknownCtor = (props: InputEditorProps) => new UnknownInputTypeInputEditor(props);
const inputEditor = InputEditor.expectedInputUiComponentCtors[this.props.expectedInput.type] || unknownCtor;
const inputEditor =
InputEditor.expectedInputUiComponentCtors[this.props.expectedInput.type] ||
InputEditor.unknownInputCtor;
const expectedInput = this.props.expectedInput;

const editorProps = {
Expand Down
4 changes: 2 additions & 2 deletions jobson/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
<parent>
<groupId>com.github.jobson</groupId>
<artifactId>jobson-project</artifactId>
<version>1.0.8</version>
<version>1.0.9</version>
</parent>

<artifactId>jobson</artifactId>
<version>1.0.8</version>
<version>1.0.9</version>
<packaging>jar</packaging>

<dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.github.jobson.jobinputs.f32.F32ExpectedInput;
import com.github.jobson.jobinputs.f64.F64ExpectedInput;
import com.github.jobson.jobinputs.file.FileExpectedInput;
import com.github.jobson.jobinputs.i32.I32ExpectedInput;
import com.github.jobson.jobinputs.i64.I64ExpectedInput;
import com.github.jobson.jobinputs.select.SelectExpectedInput;
Expand All @@ -49,6 +50,7 @@
@JsonSubTypes.Type(value = F64ExpectedInput.class, name = "double"),
@JsonSubTypes.Type(value = I32ExpectedInput.class, name = "int"),
@JsonSubTypes.Type(value = I64ExpectedInput.class, name = "long"),
@JsonSubTypes.Type(value = FileExpectedInput.class, name = "file"),
})
public abstract class JobExpectedInput<TJobInput extends JobInput> {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package com.github.jobson.jobinputs.file;

import com.github.jobson.jobinputs.JobExpectedInput;
import com.github.jobson.utils.ValidationError;

import java.util.List;
import java.util.Optional;

public final class FileExpectedInput extends JobExpectedInput<FileInput> {

@Override
public Class<FileInput> getExpectedInputClass() {
return FileInput.class;
}

@Override
public Optional<List<ValidationError>> validate(FileInput input) {
return Optional.empty();
}

@Override
public FileInput generateExampleInput() {
// A file containing "Hello, world!" as a b64-encoded string
return new FileInput("hello-world.txt", "SGVsbG8sIHdvcmxkIQo=");
}
}
Loading

0 comments on commit d191550

Please sign in to comment.