result method

StubbleResult result(
  1. StubbleContext context
)

Implementation

StubbleResult result(StubbleContext context) {
  final result = StubbleResult();

  if (_path.isEmpty) {
    result.err = StubbleError(
      text: 'With block required path to context data',
      code: errorPathNotSpecified,
    );
  } else {
    try {
      final data = context.get(_path);

      if (data != null) {
        if (data is Map) {
          final fn = context.compile(_body);

          return StubbleResult(
            result: fn != null ? fn(data) : '',
            pop: true,
          );
        } else {
          result.err = StubbleError(
            text: '"With" block data should have "Map" type',
            code: errorWithDataMalformed,
          );
        }
      } else {
        result.err = StubbleError(
          text: 'Can\'t get data from context by path "$_path"',
          code: errorPathWrongSpecified,
        );
      }
    } catch (e) {
      result.err = StubbleError(
        text: e.toString(),
        code: errorCallingHelper,
      );
    }
  }

  return result;
}