-
Notifications
You must be signed in to change notification settings - Fork 240
feat: cleaner python requests json snippets #189
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
Merged
develohpanda
merged 5 commits into
Kong:master
from
erunion:feat/cleaner-python-json-usage
Dec 1, 2020
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
39d6773
feat: cleaner python json snippets
erunion d097f94
docs: updating a method docblock
erunion a5b7073
Merge branch 'master' into feat/cleaner-python-json-usage
erunion b0838d1
Merge branch 'master' into feat/cleaner-python-json-usage
develohpanda 78d8902
test: fixing a broken test
erunion 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| 'use strict' | ||
|
|
||
| var util = require('util') | ||
|
|
||
| /** | ||
| * Create an string of given length filled with blank spaces | ||
| * | ||
| * @param {number} length Length of the array to return | ||
| * @param {string} str String to pad out with | ||
| */ | ||
| function buildString (length, str) { | ||
| return Array.apply(null, new Array(length)).map(String.prototype.valueOf, str).join('') | ||
| } | ||
|
|
||
| /** | ||
| * Create a string corresponding to a Dictionary or Array literal representation with pretty option | ||
| * and indentation. | ||
| */ | ||
| function concatValues (concatType, values, pretty, indentation, indentLevel) { | ||
| var currentIndent = buildString(indentLevel, indentation) | ||
| var closingBraceIndent = buildString(indentLevel - 1, indentation) | ||
| var join = pretty ? ',\n' + currentIndent : ', ' | ||
| var openingBrace = concatType === 'object' ? '{' : '[' | ||
| var closingBrace = concatType === 'object' ? '}' : ']' | ||
|
|
||
| if (pretty) { | ||
| return openingBrace + '\n' + currentIndent + values.join(join) + '\n' + closingBraceIndent + closingBrace | ||
| } else { | ||
| return openingBrace + values.join(join) + closingBrace | ||
| } | ||
| } | ||
|
|
||
| module.exports = { | ||
| /** | ||
| * Create a valid Python string of a literal value according to its type. | ||
| * | ||
| * @param {*} value Any JavaScript literal | ||
| * @param {Object} opts Target options | ||
| * @return {string} | ||
| */ | ||
| literalRepresentation: function (value, opts, indentLevel) { | ||
| indentLevel = indentLevel === undefined ? 1 : indentLevel + 1 | ||
|
|
||
| switch (Object.prototype.toString.call(value)) { | ||
| case '[object Number]': | ||
| return value | ||
|
|
||
| case '[object Array]': | ||
| var pretty = false | ||
| var valuesRepresentation = value.map(function (v) { | ||
| // Switch to prettify if the value is a dictionary with multiple keys | ||
| if (Object.prototype.toString.call(v) === '[object Object]') { | ||
| pretty = Object.keys(v).length > 1 | ||
| } | ||
| return this.literalRepresentation(v, opts, indentLevel) | ||
| }.bind(this)) | ||
| return concatValues('array', valuesRepresentation, pretty, opts.indent, indentLevel) | ||
|
|
||
| case '[object Object]': | ||
| var keyValuePairs = [] | ||
| for (var k in value) { | ||
| keyValuePairs.push(util.format('"%s": %s', k, this.literalRepresentation(value[k], opts, indentLevel))) | ||
| } | ||
| return concatValues('object', keyValuePairs, opts.pretty && keyValuePairs.length > 1, opts.indent, indentLevel) | ||
|
|
||
| case '[object Null]': | ||
| return 'None' | ||
|
|
||
| case '[object Boolean]': | ||
| return value ? 'True' : 'False' | ||
|
|
||
| default: | ||
| if (value === null || value === undefined) { | ||
| return '' | ||
| } | ||
| return '"' + value.toString().replace(/"/g, '\\"') + '"' | ||
| } | ||
| } | ||
| } | ||
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
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
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
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
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
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.