quote_buffer 0.1.0

  • Readme
  • Changelog
  • Example
  • Installing
  • 57

Quote Buffer #

Build Status

Introduction #

Automated code generation can be used to eliminate the need of writing and maintaining repetitive parts of a software. In order to add a string literal to generated source code, it is required to create strings whose content is delimited by escaped quotation marks.

In the following such strings are called quoted strings. QuoteBuffer simplifies adding quoted strings to a string buffer.

Usage #

QuoteBuffer extends Dart's StringBuffer. It adds the following methods for writing quoted strings to the buffer:

  • writeQ(Object obj): Converts an object to a quoted string.
    var buffer = QuoteBuffer();
    // Adds:'\'29\''
    buffer.writeQ(29);
    
  • writelnQ(Object obj): Converts an object to a quoted string followed by a newline symbol.
    // Adds:'\'name\'\n'
    buffer.writelnQ('name');
    
  • writeAllQ(Iterable<Object> objects,[String separator]): Converts a list of objects to a quoted string. Separators are optional.
    // Adds:'\'one,two\''
    buffer.writeAllQ(['one','two'], ',');
    
  • writelnAllQ(Iterable<Object> objects,[String separator]): Converts each object in the list of objects to a quoted string followed by a newline symbol. Separators are optional.
    // Adds:
    // '\'one\';\n'
    // '\'two\';\n'
    // '\'three\'\n'
    buffer.writelnAllQ(['one','two','three'], ';');
    

Examples #

The examples located in the folder example show how to use the class QuoteBuffer to simplify the generation of string literals whose content is enclosed by escaped quotation marks. One of the programs demonstrates how to use QuoteBuffer in the context of code generation.

Features and bugs #

Please file feature requests and bugs at the issue tracker.

0.0.1 #

Initial Version of the library.

0.0.2 #

Included file CHANGELOG.md.

0.0.3 #

Amended variable annotations.

0.0.4 #

Removed dependency on built_collection.

0.0.5 #

Amended README.md in folder example.

0.0.6 #

Amended README.md.

0.0.7 #

Formatted method list in README.md.

0.1.0 #

Removed dependency on generic_enum.

example/README.md

Quote Buffer #

Build Status

Examples #

The examples located in this folder show how to use the class QuoteBuffer to simplify the generation of string literals whose content is enclosed by escaped quotation marks.

Using a program it is easy to demonstrate how these methods work. The program can be run in a terminal by navigating to the folder example in your local copy of this library and using the command:

$ dart example.dart
import 'package:quote_buffer/quote_buffer.dart';

main(List<String> args) {
  List<String> strings = ['one', 'two', 'three', 'four'];
  strings.addAll(args);

  var buffer = QuoteBuffer();

  // Adds:'\'1\'' (Note: Objects are first converted to strings.)
  buffer.writeQ(1);

  // Adds a new line
  buffer.writeln('');

  // Adds:'\'name\'\n'
  buffer.writelnQ('name');

  // Adds:'\'one,two,three,four\''
  buffer.writeAllQ(strings, ',');

  // Adds a new line
  buffer.writeln('');

  // Adds:'\'======\'\n'
  buffer.writelnQ('======');

  // Adds:
  // '\'one\';\n'
  // '\'two\';\n'
  // '\'three\';\n''
  // '\'four\'\n'
  buffer.writelnAllQ(strings, ';');

  // Prints:
  // '1'
  // 'name'
  // 'one,two,three,four'
  // '======'
  // 'one';
  // 'two';
  // 'three';
  // 'four'
  print(buffer.toString());

  buffer = QuoteBuffer(
    delimiter: QuotationMark.Double,
  );

  // Adds:
  // '\"one,two,three,four\"'
  buffer.writeAllQ(strings, ',');

  // Prints:
  // "one,two,three,four"
  print(buffer.toString());
}

Code Generation #

The intended use of QuoteBuffer is with source code generating packages like code_builder. Function _init below shows how to generate a quoted string that represents a valid multi-line Dart String literal.

import 'package:code_builder/code_builder.dart';
import 'package:quote_buffer/quote_buffer.dart';
import 'package:dart_style/dart_style.dart';
import 'package:meta/meta.dart';

main(List<String> args) {

  // Column Info:
  // Keys representing column name,
  // Values representing column type and constraints.
  Map<String, String> columnMap = {
    'id': 'INTEGER PRIMARY KEY',
    'firstName': 'TEXT NOT NULL',
    'lastName': 'TEXT NOT NULL',
  };

  // Generate field
  Field init = _init(
    tableName: 'User',
    columnMap: columnMap,
  );

  // Generate source code
  String sourceCode = init.accept(DartEmitter()).toString();

  // Format source code
  String formatedCode = DartFormatter().format(sourceCode);

  // Prints:
  // /// Statement used to initialize the table User.
  // final String init = 'CREATE TABLE User ('
  //     'id INTEGER PRIMARY KEY, '
  //     'firstName TEXT NOT NULL, '
  //     'lastName TEXT NOT NULL'
  //     ')';
  print(formatedCode);
}

/// Creates source code representing a field of type [String] and name 'init'.
Field _init(
    {@required String tableName, @required Map<String, String> columnMap}) {
  var buffer = QuoteBuffer();

  // Specify table name
  // Start of multi-line string literal ---------------- Usage of QuoteBuffer
  buffer.writelnQ('CREATE TABLE $tableName (');

  // Specify columns
  List<String> columns = [];
  // Iterate over each entry in columnMap
  for (MapEntry<String, String> column in columnMap.entries) {
    columns.add('${column.key} ${column.value}');
  }

  // stringlnAll adds a newline symbol ----------------- Usage of QuoteBuffer
  // and a separator after each column entry.
  buffer.writelnAllQ(columns, ',');

  // End of multi-line string literal ------------------ Usage of QuoteBuffer
  buffer.writelnQ(')');

  return Field((b) => b
    ..modifier = FieldModifier.final$
    ..name = 'init'
    ..assignment = Code(buffer.toString())
    ..type = refer('String')
    ..docs.add('/// Statement used to initialize the '
        'table $tableName.'));
}

The program above can be run in a terminal by navigating to the folder example in your local copy of this library and using the command:

$ dart example_code_gen.dart

The expected output is:

/// Statement used to initialize the table User.
final String init = 'CREATE TABLE User ('
    'id INTEGER PRIMARY KEY,'
    'firstName TEXT NOT NULL,'
    'lastName TEXT NOT NULL'
    ')';

Features and bugs #

Please file feature requests and bugs at the issue tracker.

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  quote_buffer: ^0.1.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:quote_buffer/quote_buffer.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
13
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
57
Learn more about scoring.

We analyzed this package on May 24, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.1
  • pana: 0.13.8-dev

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.3.0 <3.0.0
Dev dependencies
test ^1.11.0