Skip to content

Commit 922f32c

Browse files
author
Alan Christie
committed
feat: Add Job traits (combine)
1 parent 45dcefe commit 922f32c

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

decoder/decoder.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
import jsonschema
1414
import yaml
1515

16+
# Supported 'traits'
17+
TRAIT_COMBINE: str = "combine"
18+
ALL_TRAITS: Set[str] = {TRAIT_COMBINE}
19+
1620
# The decoding engine implementations.
1721
# The modules are expected to be called 'decode_<TextEncoding.lower()>'
1822
from . import decode_jinja2_3_0
@@ -360,6 +364,12 @@ def get_image(job_definition: Dict[str, Any]) -> Tuple[str, str]:
360364
return image_name, image_tag
361365

362366

367+
def get_traits(job_definition: Dict[str, Any]) -> List[Dict[str, Any]]:
368+
"""Returns the array of Job traits. Each trait proeprty name can be found
369+
in the module's ALL_TRAITS set."""
370+
return job_definition.get("traits", [])
371+
372+
363373
def decode(
364374
template_text: str,
365375
variable_map: Optional[Dict[str, str]],

decoder/job-definition-schema.yaml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,13 @@ definitions:
101101
$ref: '#/definitions/job-test'
102102
replaces:
103103
$ref: '#/definitions/replace-list'
104+
# Traits - a list of Job characteristics or behaviours.
105+
# These can be used by automated tools to drive the Job.
106+
traits:
107+
type: array
108+
items:
109+
anyOf:
110+
- $ref: '#/definitions/trait-combine'
104111
required:
105112
- name
106113
- version
@@ -181,6 +188,26 @@ definitions:
181188
type: string
182189
pattern: '^([1-9][0-9]{2,3}Mi|[1-9][0-9]{0,1}Gi)$'
183190

191+
# The 'combine' trait.
192+
# This declares the name of an input that is typically
193+
# a series of files that the job combines, producing a single output.
194+
# A job with a 'combine' trait is esentially a "combiner" - somewthing
195+
# that processes a number of files, all presented to the Job via
196+
# the named variable. If the Job combines files using its "--source"
197+
# input property it would have a "combine: source" trait.
198+
#
199+
# The value of the "combine" property should be the name of an
200+
# input variable declared in the Job's "variables" block.
201+
trait-combine:
202+
type: object
203+
additionalProperties: false
204+
properties:
205+
combine:
206+
type: string
207+
pattern: '^[a-zA-Z]{1}[a-zA-Z0-9-_]{0,79}$'
208+
required:
209+
- combine
210+
184211
# Image environment definitions.
185212
environment:
186213
type: array

tests/test_get_traits.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Tests for the decoder's get_traits() function.
2+
from typing import Dict
3+
4+
import pytest
5+
6+
pytestmark = pytest.mark.unit
7+
8+
from decoder import decoder
9+
10+
11+
def test_check_all_traits():
12+
# Arrange
13+
14+
# Act
15+
16+
# Assert
17+
assert len(decoder.ALL_TRAITS) == 1
18+
assert decoder.TRAIT_COMBINE in decoder.ALL_TRAITS
19+
20+
21+
def test_get_traits_when_none():
22+
# Arrange
23+
job_definition: Dict = {}
24+
25+
# Act
26+
traits = decoder.get_traits(job_definition)
27+
28+
# Assert
29+
assert traits == []
30+
31+
32+
def test_get_traits_when_combine():
33+
# Arrange
34+
job_definition: Dict = {"traits": [{"combine": "source"}]}
35+
36+
# Act
37+
traits = decoder.get_traits(job_definition)
38+
39+
# Assert
40+
assert len(traits) == 1
41+
assert traits[0] == {"combine": "source"}

0 commit comments

Comments
 (0)