MCPcopy Create free account
hub / github.com/ampproject/amphtml / ValueToString

Function ValueToString

validator/validator_gen_js.py:297–320  ·  view source on GitHub ↗

For a non-repeated field, renders the value as a Javascript literal. Args: descriptor: The descriptor module from the protobuf package, e.g. google.protobuf.descriptor. field_desc: The type descriptor for the field value to be rendered. value: The value of the non-repeated fie

(descriptor, field_desc, value)

Source from the content-addressed store, hash-verified

295
296
297def ValueToString(descriptor, field_desc, value):
298 """For a non-repeated field, renders the value as a Javascript literal.
299
300 Args:
301 descriptor: The descriptor module from the protobuf package, e.g.
302 google.protobuf.descriptor.
303 field_desc: The type descriptor for the field value to be rendered.
304 value: The value of the non-repeated field to be rendered.
305 Returns:
306 A Javascript literal for the provided non-repeated value.
307 """
308 if field_desc.type == descriptor.FieldDescriptor.TYPE_STRING:
309 escaped = ('' + value).encode('unicode-escape')
310 return "'%s'" % escaped.decode().replace("'", "\\'")
311 if field_desc.type == descriptor.FieldDescriptor.TYPE_BOOL:
312 if value:
313 return 'true'
314 return 'false'
315 if field_desc.type == descriptor.FieldDescriptor.TYPE_ENUM:
316 enum_value_name = field_desc.enum_type.values_by_number[value].name
317 return '%s.%s' % (field_desc.enum_type.full_name, enum_value_name)
318 if value is None:
319 return 'null'
320 return str(value)
321
322CONSTRUCTOR_ARG_FIELDS = [
323 'amp.validator.AmpLayout.supported_layouts',

Callers 1

AssignedValueForFunction · 0.85

Calls 2

replaceMethod · 0.45
decodeMethod · 0.45

Tested by

no test coverage detected