encode static method

List<int> encode(
  1. Object value,
  2. ProtoFieldConfig config, {
  3. bool packed = false,
})

Implementation

static List<int> encode(
  Object value,
  ProtoFieldConfig config, {
  bool packed = false,
}) {
  try {
    final encoding = config.fieldType;
    return switch (encoding) {
      ProtoFieldType.bytes => _encodeBytes(
        JsonParser.valueAsBytes(value, allowHex: true),
        config,
      ),
      ProtoFieldType.enumType => _encodeEnum(value, config, packed: packed),
      ProtoFieldType.string => _encodeBytes(
        JsonParser.valueAsBytes(
          JsonParser.valueAsString(value),
          allowHex: false,
          encoding: StringEncoding.utf8,
        ),
        config,
      ),
      ProtoFieldType.float || ProtoFieldType.double => _encodeDouble(
        JsonParser.valueAsDouble<double>(value),
        config,
        packed: packed,
      ),
      ProtoFieldType.fixed32 || ProtoFieldType.sFixed32 => _encodeFixed32(
        JsonParser.valueAsInt(value),
        config,
        packed: packed,
      ),
      ProtoFieldType.fixed64 || ProtoFieldType.sFixed64 => _encodeFixed64(
        JsonParser.valueAsBigInt(value),
        config,
        packed: packed,
      ),
      ProtoFieldType.int32 || ProtoFieldType.uint32 => _encodeInt32(
        JsonParser.valueAsInt(value),
        config,
        packed: packed,
      ),
      ProtoFieldType.int64 || ProtoFieldType.uint64 => _encodeInt64(
        JsonParser.valueAsBigInt(value),
        config,
        packed: packed,
      ),
      ProtoFieldType.sint32 => _encodeSint32(
        JsonParser.valueAsInt(value),
        config,
        packed: packed,
      ),
      ProtoFieldType.sint64 => _encodeSint64(
        JsonParser.valueAsBigInt(value),
        config,
        packed: packed,
      ),
      ProtoFieldType.bool => _encodeBool(
        JsonParser.valueAsBool(value),
        config,
        packed: packed,
      ),
      ProtoFieldType.message => _ecodableMessage(
        JsonParser.valueAs(value),
        config,
      ),
      ProtoFieldType.repeated => _encodeList(
        JsonParser.valueEnsureAsList(value),
        config,
      ),
      ProtoFieldType.map => _encodeMap(JsonParser.valueAsMap(value), config),
    };
  } on JSONHelperException {
    throw ArgumentException.invalidOperationArguments(
      "encode",
      reason: "Invalid value provided for field number ${config.fieldNumber}",
      details: {"value": value},
    );
  }
}