<commit_before>#
#  Copyright 2014 MongoDB, Inc.
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#  http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.
#

package MongoDB::Role::_WriteResult;

# MongoDB interface for common write result attributes and methods

use version;
our $VERSION = 'v0.999.999.4'; # TRIAL

use MongoDB::Error;
use MongoDB::_Constants;
use MongoDB::_Types -types;
use Types::Standard -types;
use Moo::Role;
use namespace::clean;

has acknowledged => (
    is       => 'ro',
    required => 1,
    isa      => Bool,
);

has [qw/write_errors write_concern_errors/] => (
    is       => 'ro',
    required => 1,
    isa      => ArrayOfHashRef,
);

with 'MongoDB::Role::_LastError';

# inline assert_no_write_error and assert_no_write_concern rather
# than having to make to additional method calls
sub assert {
    my ($self) = @_;

    $self->_throw_database_error("MongoDB::WriteError")
      if $self->count_write_errors;

    if ( $self->count_write_concern_errors ) {
        MongoDB::WriteConcernError->throw(
            message => $self->last_errmsg,
            result  => $self,
            code    => WRITE_CONCERN_ERROR,
        );
    }

    return 1;
}

sub assert_no_write_error {
    my ($self) = @_;

    $self->_throw_database_error("MongoDB::WriteError")
      if $self->count_write_errors;
    return 1;
}

sub assert_no_write_concern_error {
    my ($self) = @_;
    if ( $self->count_write_concern_errors ) {
        MongoDB::WriteConcernError->throw(
            message => $self->last_errmsg,
            result  => $self,
            code    => WRITE_CONCERN_ERROR,
        );
    }
    return 1;
}

sub count_write_errors {
    my ($self) = @_;
    return scalar @{ $self->write_errors };
}

sub count_write_concern_errors {
    my ($self) = @_;
    return scalar @{ $self->write_concern_errors };
}

sub last_errmsg {
    my ($self) = @_;
    if ( $self->count_write_errors ) {
        return $self->write_errors->[-1]{errmsg};
    }
    elsif ( $self->count_write_concern_errors ) {
        return $self->write_concern_errors->[-1]{errmsg};
    }
    else {
        return "";
    }
}

sub last_code {
    my ($self) = @_;
    if ( $self->count_write_errors ) {
        return $self->write_errors->[-1]{code} || UNKNOWN_ERROR;<commit_msg>Optimizes _WriteResult assertions<commit_after>#
#  Copyright 2014 MongoDB, Inc.
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#  http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.
#

package MongoDB::Role::_WriteResult;

# MongoDB interface for common write result attributes and methods

use version;
our $VERSION = 'v0.999.999.4'; # TRIAL

use MongoDB::Error;
use MongoDB::_Constants;
use MongoDB::_Types -types;
use Types::Standard -types;
use Moo::Role;
use namespace::clean;

has acknowledged => (
    is       => 'ro',
    required => 1,
    isa      => Bool,
);

has [qw/write_errors write_concern_errors/] => (
    is       => 'ro',
    required => 1,
    isa      => ArrayOfHashRef,
);

with 'MongoDB::Role::_LastError';

# inline assert_no_write_error and assert_no_write_concern rather
# than having to make to additional method calls
sub assert {
    my ($self) = @_;

    $self->_throw_database_error("MongoDB::WriteError")
      if scalar @{ $self->write_errors };

    MongoDB::WriteConcernError->throw(
        message => $self->last_errmsg,
        result  => $self,
        code    => WRITE_CONCERN_ERROR,
    ) if scalar @{ $self->write_concern_errors };

    return $self;
}

sub assert_no_write_error {
    my ($self) = @_;

    $self->_throw_database_error("MongoDB::WriteError")
      if scalar @{ $self->write_errors };

    return $self;
}

sub assert_no_write_concern_error {
    my ($self) = @_;

    MongoDB::WriteConcernError->throw(
        message => $self->last_errmsg,
        result  => $self,
        code    => WRITE_CONCERN_ERROR,
    ) if scalar @{ $self->write_concern_errors };

    return $self;
}

sub count_write_errors {
    my ($self) = @_;
    return scalar @{ $self->write_errors };
}

sub count_write_concern_errors {
    my ($self) = @_;
    return scalar @{ $self->write_concern_errors };
}

sub last_errmsg {
    my ($self) = @_;
    if ( $self->count_write_errors ) {
        return $self->write_errors->[-1]{errmsg};
    }
    elsif ( $self->count_write_concern_errors ) {
        return $self->write_concern_errors->[-1]{errmsg};
    }
    else {
        return "";
    }
}

sub last_code {
    my ($self) = @_;
    if ( $self->count_write_errors ) {
        return $self->write_errors->[-1]{code} || UNKNOWN_ERROR;
{'fraction_non_alphanumeric': 0.12483959670027497, 'fraction_numerical': 0.008249312557286892, 'mean_word_length': 3.0838323353293413, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 73, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5915292', 'n_tokens_mistral': 1809, 'n_tokens_neox': 1760, 'n_words': 455}