<commit_before># -*- coding: utf-8 -*-
# Copyright 2015 Google 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.

import base64
import datetime
import json
import sys

import unittest2

from apitools.base.protorpclite import message_types
from apitools.base.protorpclite import messages
from apitools.base.protorpclite import util
from apitools.base.py import encoding
from apitools.base.py import exceptions
from apitools.base.py import extra_types


class SimpleMessage(messages.Message):
    field = messages.StringField(1)
    repfield = messages.StringField(2, repeated=True)


class BytesMessage(messages.Message):
    field = messages.BytesField(1)
    repfield = messages.BytesField(2, repeated=True)


class TimeMessage(messages.Message):
    timefield = message_types.DateTimeField(3)


@encoding.MapUnrecognizedFields('additional_properties')
class AdditionalPropertiesMessage(messages.Message):

    class AdditionalProperty(messages.Message):
        key = messages.StringField(1)
        value = messages.StringField(2)

    additional_properties = messages.MessageField(
        'AdditionalProperty', 1, repeated=True)


@encoding.MapUnrecognizedFields('additional_properties')
class AdditionalIntPropertiesMessage(messages.Message):

    class AdditionalProperty(messages.Message):
        key = messages.StringField(1)
        value = messages.IntegerField(2)

    additional_properties = messages.MessageField(
        'AdditionalProperty', 1, repeated=True)


@encoding.MapUnrecognizedFields('additional_properties')
class UnrecognizedEnumMessage(messages.Message):

    class ThisEnum(messages.Enum):
        VALUE_ONE = 1
        VALUE_TWO = 2

    class AdditionalProperty(messages.Message):
        key = messages.StringField(1)
        value = messages.EnumField('UnrecognizedEnumMessage.ThisEnum', 2)

    additional_properties = messages.MessageField(
        AdditionalProperty, 1, repeated=True)


class CompoundPropertyType(messages.Message):
    index = messages.IntegerField(1)
    name = messages.StringField(2)


class MessageWithEnum(messages.Message):

    class ThisEnum(messages.Enum):
        VALUE_ONE = 1
        VALUE_TWO = 2

    field_one = messages.EnumField(ThisEnum, 1)
    field_two = messages.EnumField(ThisEnum, 2, default=ThisEnum.VALUE_TWO)
    ignored_field = messages.EnumField(ThisEnum, 3)


@encoding.MapUnrecognizedFields('additional_properties')
class AdditionalMessagePropertiesMessage(messages.Message):

    class AdditionalProperty(messages.Message):
        key = messages.StringField(1)
        value = messages.MessageField(CompoundPropertyType, 2)

    additional_properties = messages.MessageField(
        'AdditionalProperty', 1, repeated=True)


@encoding.MapUnrecognizedFields('additionalProperties')
class MapToBytesValue(messages.Message):
    class AdditionalProperty(messages.Message):
        key = messages.StringField(1)
        value = messages.BytesField(2)

    additionalProperties = messages.MessageField('AdditionalProperty', 1,
                                                 repeated=True)


class HasNestedMessage(messages.Message):
    nested = messages.MessageField(AdditionalPropertiesMessage, 1)
    nested_list = messages.StringField(2, repeated=True)


class ExtraNestedMessage(messages.Message):
    nested = messages.MessageField(HasNestedMessage, 1)


class MessageWithRemappings(messages.Message):

    class SomeEnum(messages.Enum):
        enum_value = 1
        second_value = 2

    enum_field = messages.EnumField(SomeEnum, 1)
    double_encoding = messages.EnumField(SomeEnum, 2)
    another_field = messages.StringField(3)
    repeated_enum = messages.EnumField(SomeEnum, 4, repeated=True)
    repeated_field = messages.StringField(5, repeated=True)


class MessageWithPackageAndRemappings(messages.Message):

    class SomeEnum(messages.Enum):
        enum_value = 1
        second_value = 2

    enum_field = messages.EnumField(SomeEnum, 1)
    another_field = messages.StringField(2)


@encoding.MapUnrecognizedFields('additional_properties')
class RepeatedJsonValueMessage(messages.Message):

    class AdditionalProperty(messages.Message):
        key = messages.StringField(1)
        value = messages.MessageField(extra_types.JsonValue, 2, repeated=True)

    additional_properties = messages.MessageField('AdditionalProperty', 1,
                                                  repeated=True)


encoding.AddCustomJsonEnumMapping(MessageWithRemappings.SomeEnum,
                                  'enum_value', 'wire_name')
encoding.AddCustomJsonFieldMapping(MessageWithRemappings,
                                   'double_encoding', 'doubleEncoding')
encoding.AddCustomJsonFieldMapping(MessageWithRemappings,
                                   'another_field', 'anotherField')
encoding.AddCustomJsonFieldMapping(MessageWithRemappings,
                                   'repeated_field', 'repeatedField')


class EncodingTest(unittest2.TestCase):

    def testCopyProtoMessage(self):<commit_msg>Make JSON mappings use the class as a dictionary key, not a string identifier.<commit_after># -*- coding: utf-8 -*-
# Copyright 2015 Google 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.

import base64
import datetime
import json
import sys

import unittest2

from apitools.base.protorpclite import message_types
from apitools.base.protorpclite import messages
from apitools.base.protorpclite import util
from apitools.base.py import encoding
from apitools.base.py import exceptions
from apitools.base.py import extra_types


class SimpleMessage(messages.Message):
    field = messages.StringField(1)
    repfield = messages.StringField(2, repeated=True)


class BytesMessage(messages.Message):
    field = messages.BytesField(1)
    repfield = messages.BytesField(2, repeated=True)


class TimeMessage(messages.Message):
    timefield = message_types.DateTimeField(3)


@encoding.MapUnrecognizedFields('additional_properties')
class AdditionalPropertiesMessage(messages.Message):

    class AdditionalProperty(messages.Message):
        key = messages.StringField(1)
        value = messages.StringField(2)

    additional_properties = messages.MessageField(
        'AdditionalProperty', 1, repeated=True)


@encoding.MapUnrecognizedFields('additional_properties')
class AdditionalIntPropertiesMessage(messages.Message):

    class AdditionalProperty(messages.Message):
        key = messages.StringField(1)
        value = messages.IntegerField(2)

    additional_properties = messages.MessageField(
        'AdditionalProperty', 1, repeated=True)


@encoding.MapUnrecognizedFields('additional_properties')
class UnrecognizedEnumMessage(messages.Message):

    class ThisEnum(messages.Enum):
        VALUE_ONE = 1
        VALUE_TWO = 2

    class AdditionalProperty(messages.Message):
        key = messages.StringField(1)
        value = messages.EnumField('UnrecognizedEnumMessage.ThisEnum', 2)

    additional_properties = messages.MessageField(
        AdditionalProperty, 1, repeated=True)


class CompoundPropertyType(messages.Message):
    index = messages.IntegerField(1)
    name = messages.StringField(2)


class MessageWithEnum(messages.Message):

    class ThisEnum(messages.Enum):
        VALUE_ONE = 1
        VALUE_TWO = 2

    field_one = messages.EnumField(ThisEnum, 1)
    field_two = messages.EnumField(ThisEnum, 2, default=ThisEnum.VALUE_TWO)
    ignored_field = messages.EnumField(ThisEnum, 3)


@encoding.MapUnrecognizedFields('additional_properties')
class AdditionalMessagePropertiesMessage(messages.Message):

    class AdditionalProperty(messages.Message):
        key = messages.StringField(1)
        value = messages.MessageField(CompoundPropertyType, 2)

    additional_properties = messages.MessageField(
        'AdditionalProperty', 1, repeated=True)


@encoding.MapUnrecognizedFields('additionalProperties')
class MapToBytesValue(messages.Message):
    class AdditionalProperty(messages.Message):
        key = messages.StringField(1)
        value = messages.BytesField(2)

    additionalProperties = messages.MessageField('AdditionalProperty', 1,
                                                 repeated=True)


class HasNestedMessage(messages.Message):
    nested = messages.MessageField(AdditionalPropertiesMessage, 1)
    nested_list = messages.StringField(2, repeated=True)


class ExtraNestedMessage(messages.Message):
    nested = messages.MessageField(HasNestedMessage, 1)


class MessageWithRemappings(messages.Message):

    class SomeEnum(messages.Enum):
        enum_value = 1
        second_value = 2

    enum_field = messages.EnumField(SomeEnum, 1)
    double_encoding = messages.EnumField(SomeEnum, 2)
    another_field = messages.StringField(3)
    repeated_enum = messages.EnumField(SomeEnum, 4, repeated=True)
    repeated_field = messages.StringField(5, repeated=True)


class MessageWithPackageAndRemappings(messages.Message):

    class SomeEnum(messages.Enum):
        enum_value = 1
        second_value = 2

    enum_field = messages.EnumField(SomeEnum, 1)
    another_field = messages.StringField(2)


@encoding.MapUnrecognizedFields('additional_properties')
class RepeatedJsonValueMessage(messages.Message):

    class AdditionalProperty(messages.Message):
        key = messages.StringField(1)
        value = messages.MessageField(extra_types.JsonValue, 2, repeated=True)

    additional_properties = messages.MessageField('AdditionalProperty', 1,
                                                  repeated=True)

encoding.AddCustomJsonEnumMapping(MessageWithRemappings.SomeEnum,
                                  'enum_value', 'wire_name')
encoding.AddCustomJsonFieldMapping(MessageWithRemappings,
                                   'double_encoding', 'doubleEncoding')
encoding.AddCustomJsonFieldMapping(MessageWithRemappings,
                                   'another_field', 'anotherField')
encoding.AddCustomJsonFieldMapping(MessageWithRemappings,
                                   'repeated_field', 'repeatedField')


class EncodingTest(unittest2.TestCase):

    def testCopyProtoMessage(self):
{'fraction_non_alphanumeric': 0.08334836731012088, 'fraction_numerical': 0.010644055565578207, 'mean_word_length': 3.901414677276746, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2342526', 'n_tokens_mistral': 3005, 'n_tokens_neox': 2804, 'n_words': 648}