estimate property

List<String> estimate

Returns code required to estimate size in bytes of this field.

Implementation

List<String> get estimate {
    return <String>[
        if (optional) '\$setFlag($name != null);',
        if (optional) 'if ($name != null) {',
            if (!array) ...<String>[
                if (binary) 'bytes += $binaryLength;'
                else if ((type is String || type is Enum) && type != 'string') 'bytes += ${sizeOf(type)};'
                else if (type == 'string') 'bytes += \$stringBytes($_name);'
                else if (type is Message) 'bytes += $_name.\$estimate();'
                else throw Exception('Wrong type "$type" for field "$name" in "${message.filename}".')
            ]
            else ...<String>[
                'bytes += 4;',
                if (binary) 'bytes += $binaryLength * $_name.length;'
                else if ((type is String || type is Enum) && type != 'string') 'bytes += ${sizeOf(type)} * $_name.length;'
                else if (type == 'string') 'for (int i = 0; i < $_name.length; i++) bytes += \$stringBytes($_name[i]);'
                else if (type is Message) 'for (int i = 0; i < $_name.length; i++) bytes += $_name[i].\$estimate();'
                else throw Exception('Wrong type "$type" for field "$name" in "${message.filename}".')
            ],
        if (optional) '}',
    ];
}