Repository navigation
MaD: update generator scripts to support writing JSON data extensions #22798
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nickrolfe
wants to merge
2
commits into
main
Choose a base branch
from
nickrolfe/mad-generator-json
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+215
−141
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,76 @@ | ||
| import json | ||
|
|
||
|
|
||
| class DataExtensionFile: | ||
| """Simple class for representing a data extension file and writing it to disk in a | ||
| pretty-printed YAML or JSON format.""" | ||
|
|
||
| def __init__(self, pack): | ||
| self.pack = pack | ||
| # predicate -> dict of tuples (used as an ordered set) | ||
| self.predicates = {} | ||
|
|
||
| def add_rows(self, predicate, rows): | ||
| """Add the rows, which can be any collection of any collection of string/boolean values.""" | ||
| if predicate not in self.predicates: | ||
| self.predicates[predicate] = dict() | ||
| d = self.predicates[predicate] | ||
| for row in rows: | ||
| # To be agnostic about the type of collection used for rows, convert each one to a | ||
| # tuple. Insert the tuple as a key in the dict, associating it with a dummy value. | ||
| d[tuple(row)] = None | ||
|
|
||
| def yaml_for_value(self, value): | ||
| # if is boolean: | ||
| if isinstance(value, bool): | ||
| # json.dumps would produce lower-case "true" or "false". Even though that's valid YAML, | ||
| # we have historically used the upper-case variants. | ||
| return "True" if value else "False" | ||
| return json.dumps(value) | ||
|
|
||
| def yaml_for_row(self, row): | ||
| return f"[{', '.join(self.yaml_for_value(value) for value in row)}]" | ||
|
|
||
| def yaml_for_predicate(self, predicate): | ||
| return f""" - addsTo: | ||
| pack: {self.pack} | ||
| extensible: {predicate} | ||
| data: | ||
| - {'\n - '.join(self.yaml_for_row(row) for row in self.predicates[predicate].keys())} | ||
| """ | ||
|
|
||
| def write_yaml(self, f): | ||
| # It would be preferable to use a YAML serialization library, but this lets us control the | ||
| # formatting e.g. to produce one line per tuple. | ||
| f.write("# THIS FILE IS AN AUTO-GENERATED MODELS AS DATA FILE. DO NOT EDIT.\n") | ||
| f.write("extensions:\n") | ||
| for predicate in sorted(self.predicates.keys()): | ||
| f.write(self.yaml_for_predicate(predicate)) | ||
|
|
||
| def json_for_row(self, row): | ||
| return "[" + ", ".join(json.dumps(value) for value in row) + "]" | ||
|
|
||
| def json_for_predicate(self, predicate): | ||
| return f""" {{ | ||
| "addsTo": {{ | ||
| "pack": "{self.pack}", | ||
| "extensible": "{predicate}" | ||
| }}, | ||
| "data": [ | ||
| {',\n '.join(self.json_for_row(row) for row in self.predicates[predicate].keys())} | ||
| ] | ||
| }}""" | ||
|
|
||
| def write_json(self, f): | ||
| # It would be preferable to use a serializer like Python's json.dumps, but it can't | ||
| # pretty-print with one line per tuple. | ||
| f.write("// THIS FILE IS AN AUTO-GENERATED MODELS AS DATA FILE. DO NOT EDIT.\n") | ||
| f.write("{\n") | ||
| f.write(' "extensions": [\n') | ||
| for i, predicate in enumerate(self.predicates): | ||
| if i > 0: | ||
| f.write(",\n") | ||
| f.write(self.json_for_predicate(predicate)) | ||
| f.write("\n") | ||
| f.write(" ]\n") | ||
| f.write("}") | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.