memberVariables method

AFMemberVariableTemplates? memberVariables(
  1. AFCommandContext context,
  2. AFCommandArgumentsParsed args,
  3. String mainType, {
  4. bool isAugment = false,
  5. bool? isIntIdOverride,
})

Utility for parsing the --member-variables and --resolve-variables flags, if present.

Implementation

AFMemberVariableTemplates? memberVariables(AFCommandContext context, AFCommandArgumentsParsed args, String mainType, {
  bool isAugment = false,
  bool? isIntIdOverride,
}) {
  final isRoot = mainType.endsWith(AFCodeGenerator.rootSuffix);
  final isQuery = mainType.endsWith(AFGenerateQuerySubcommand.suffixQuery);
  final memberVarsSource = args.accessNamed(AFGenerateSubcommand.argMemberVariables);

  final memberVars = _parseSemicolonDeclarations(context, memberVarsSource);

  // currently, allowing them to have no member variables adds a lot of complexity, because you then have to worry about
  // adding in {} in a bunch of places.   So, for now, just force them to always have at least one variable.
  if(memberVars.isEmpty) {
    context.output.writeTwoColumns(col1: "info ", col2: "Adding tempPlaceholder member variable due to issues with 'generate augment' in scenarios with no member variables.");
    memberVars[AFMemberVariableTemplates.tempPlaceholderVarName] = "int";
  }


  // if they specified serial, then make sure they specified an ID.
  var isIntId = false;
  if(!isRoot && !isQuery && !args.accessNamedFlag(AFGenerateStateSubcommand.argNotSerial)) {
    var idType = memberVars[AFDocumentIDGenerator.columnId];
    if(idType == "int") {
      isIntId = true;
      memberVars[AFDocumentIDGenerator.columnId]= "String";
      writeConvertingIntIdMessage(context);
    }
    const errIdColumn = "You must either specify --${AFGenerateStateSubcommand.argNotSerial}, or you must specify a --${AFGenerateSubcommand.argMemberVariables} containing either 'String ${AFDocumentIDGenerator.columnId}' or 'int ${AFDocumentIDGenerator.columnId}'";

    if(!isAugment) {
      if(idType == null) {
        throw AFCommandError(error: errIdColumn);
      }

      if(idType != "int" && idType != "String") {
        throw AFCommandError(error: errIdColumn);
      }
    }
  }

  final resolveVarsSource = args.accessNamed(AFGenerateSubcommand.argResolveVariables);
  final resolveVars = _parseSemicolonDeclarations(context, resolveVarsSource);

  if(memberVars.isEmpty && resolveVars.isEmpty) {
    return null;
  }

  final withFlutterState = args.accessNamedFlag(AFGenerateUISubcommand.argWithFlutterState);

  return AFMemberVariableTemplates(
    memberVars: memberVars,
    resolveVars: resolveVars,
    isAugment: isAugment,
    isIntId: isIntIdOverride ?? isIntId,
    mainType: mainType,
    withFlutterState: withFlutterState
  );
}