From b64722cca85a0396cc1389da694e7abd2338ff2e Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Mon, 13 Apr 2015 14:18:32 +0200 Subject: [PATCH] docs(CLI): random values + cursor information * Instead of writing pod-types, we generate a random value of the required type. * Fully document how cursors can be set, which is all that's usually demonstrated in more complex dynamic structure documentation Fixes #49 --- src/mako/cli/docs/commands.md.mako | 14 +++++++++----- src/mako/cli/lib/cli.py | 16 +++++++++++++++- src/mako/lib/util.py | 4 ++-- 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/src/mako/cli/docs/commands.md.mako b/src/mako/cli/docs/commands.md.mako index 00dc095782..efc8b154b5 100644 --- a/src/mako/cli/docs/commands.md.mako +++ b/src/mako/cli/docs/commands.md.mako @@ -69,14 +69,18 @@ For example, a structure like this: ${cli_schema_to_yaml(request_cli_schema)} ``` -can be set completely with the following arguments. Note how the cursor position is adjusted to the respective fields: +can be set completely with the following arguments which are assumed to be executed in the given order. Note how the cursor position is adjusted to the respective structures, allowing simple field names to be used most of the time. ${self._list_schem_args(request_cli_schema)} -* The cursor position is always set relative to the current one, unless the field name starts with the '${FIELD_SEP}' character. Fields can be nested such as in `-r f${FIELD_SEP}s${FIELD_SEP}o` . -* **Lists** are always appended to, in the example, the list at `struct${FIELD_SEP}sub_struct${FIELD_SEP}strings` will have the value `["first", "second"]`. -* **Mappings** are set using the `key=value` form. -* You can also set nested fields without setting the cursor explicitly. For example, to set the mapping from the root, you would specify `-r struct${FIELD_SEP}sub_struct${FIELD_SEP}mapping=foo=bar`. In case the cursor is not at the root, you may explicitly drill down from the root using a leading '${FIELD_SEP}' character. +${'###'} About Cursors + +The cursor position is key to comfortably set complex nested structures. The following rules apply: + +* The cursor position is always set relative to the current one, unless the field name starts with the `${FIELD_SEP}` character. Fields can be nested such as in `-${STRUCT_FLAG} f${FIELD_SEP}s${FIELD_SEP}o` . +* The cursor position is set relative to the top-level structure if it starts with `${FIELD_SEP}`, e.g. `-${STRUCT_FLAG} ${FIELD_SEP}s${FIELD_SEP}s` +* You can also set nested fields without setting the cursor explicitly. For example, to set a value relative to the current cursor position, you would specify `-${STRUCT_FLAG} struct${FIELD_SEP}sub_struct=bar`. +* You can move the cursor one level up by using `${FIELD_SEP}${FIELD_SEP}`. Each additional `${FIELD_SEP}` moves it up one additional level. E.g. `${FIELD_SEP}${FIELD_SEP}${FIELD_SEP}` would go three levels up. % endif # have request value % if mc.media_params: diff --git a/src/mako/cli/lib/cli.py b/src/mako/cli/lib/cli.py index 20944f7091..0d95467328 100644 --- a/src/mako/cli/lib/cli.py +++ b/src/mako/cli/lib/cli.py @@ -4,6 +4,7 @@ import os import re import collections from copy import deepcopy +from random import (randint, random, choice, seed) SPLIT_START = '>>>>>>>' SPLIT_END = '<<<<<<<' @@ -36,6 +37,19 @@ CTYPE_ARRAY = 'list' CTYPE_MAP = 'map' SchemaEntry = collections.namedtuple('SchemaEntry', ['container_type', 'actual_property', 'property']) +JSON_TYPE_RND_MAP = {'boolean': lambda: str(bool(randint(0, 1))).lower(), + 'integer' : lambda: randint(0, 100), + 'uint32' : lambda: randint(0, 100), + 'uint64' : lambda: randint(0, 65556), + 'float' : lambda: random(), + 'double' : lambda: random(), + 'number' : lambda: random(), + 'int32' : lambda: randint(-101, -1), + 'int64' : lambda: randint(-101, -1), + 'string': lambda: '%s' % choice(util.words).lower()} + +assert len(set(JSON_TYPE_RND_MAP.keys()) ^ POD_TYPES) == 0 + def new_method_context(resource, method, c): m = c.fqan_map[util.to_fqan(c.rtc_map[resource], resource, method)] response_schema = util.method_response(c, m) @@ -147,7 +161,7 @@ def cli_schema_to_yaml(schema, prefix=''): # Return a value string suitable for the given field. def field_to_value(f): - v = f.actual_property.type + v = JSON_TYPE_RND_MAP[f.actual_property.type]() if f.container_type == CTYPE_MAP: v = 'key=%s' % v return v diff --git a/src/mako/lib/util.py b/src/mako/lib/util.py index b4e0da3a7e..9d0b9d4bb4 100644 --- a/src/mako/lib/util.py +++ b/src/mako/lib/util.py @@ -35,7 +35,7 @@ RESERVED_WORDS = set(('abstract', 'alignof', 'as', 'become', 'box', 'break', 'co 'return', 'sizeof', 'static', 'self', 'struct', 'super', 'true', 'trait', 'type', 'typeof', 'unsafe', 'unsized', 'use', 'virtual', 'where', 'while', 'yield')) -_words = [w.strip(',') for w in "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.".split(' ')] +words = [w.strip(',') for w in "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.".split(' ')] RUST_TYPE_RND_MAP = {'bool': lambda: str(bool(randint(0, 1))).lower(), 'u32' : lambda: randint(0, 100), 'u64' : lambda: randint(0, 100), @@ -43,7 +43,7 @@ RUST_TYPE_RND_MAP = {'bool': lambda: str(bool(randint(0, 1))).lower(), 'f32' : lambda: random(), 'i32' : lambda: randint(-101, -1), 'i64' : lambda: randint(-101, -1), - 'String': lambda: '"%s"' % choice(_words), + 'String': lambda: '"%s"' % choice(words), } TREF = '$ref' IO_RESPONSE = 'response'