MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / ParseAttrValue

Function ParseAttrValue

tensorflow/core/framework/attr_value_util.cc:397–453  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

395}
396
397bool ParseAttrValue(StringPiece type, StringPiece text, AttrValue* out) {
398 // Parse type.
399 string field_name;
400 bool is_list = absl::ConsumePrefix(&type, "list(");
401 if (absl::ConsumePrefix(&type, "string")) {
402 field_name = "s";
403 } else if (absl::ConsumePrefix(&type, "int")) {
404 field_name = "i";
405 } else if (absl::ConsumePrefix(&type, "float")) {
406 field_name = "f";
407 } else if (absl::ConsumePrefix(&type, "bool")) {
408 field_name = "b";
409 } else if (absl::ConsumePrefix(&type, "type")) {
410 field_name = "type";
411 } else if (absl::ConsumePrefix(&type, "shape")) {
412 field_name = "shape";
413 } else if (absl::ConsumePrefix(&type, "tensor")) {
414 field_name = "tensor";
415 } else if (absl::ConsumePrefix(&type, "func")) {
416 field_name = "func";
417 } else if (absl::ConsumePrefix(&type, "placeholder")) {
418 field_name = "placeholder";
419 } else {
420 return false;
421 }
422 if (is_list && !absl::ConsumePrefix(&type, ")")) {
423 return false;
424 }
425
426 // Construct a valid text proto message to parse.
427 string to_parse;
428 if (is_list) {
429 // TextFormat parser considers "i: 7" to be the same as "i: [7]",
430 // but we only want to allow list values with [].
431 StringPiece cleaned = text;
432 str_util::RemoveLeadingWhitespace(&cleaned);
433 str_util::RemoveTrailingWhitespace(&cleaned);
434 if (cleaned.size() < 2 || cleaned[0] != '[' ||
435 cleaned[cleaned.size() - 1] != ']') {
436 return false;
437 }
438 cleaned.remove_prefix(1);
439 str_util::RemoveLeadingWhitespace(&cleaned);
440 if (cleaned.size() == 1) {
441 // User wrote "[]", so return empty list without invoking the TextFormat
442 // parse which returns an error for "i: []".
443 out->Clear();
444 out->mutable_list();
445 return true;
446 }
447 to_parse = strings::StrCat("list { ", field_name, ": ", text, " }");
448 } else {
449 to_parse = strings::StrCat(field_name, ": ", text);
450 }
451
452 return ProtoParseFromString(to_parse, out);
453}
454

Callers 2

FinalizeAttrFunction · 0.85
CreateNodeDefMethod · 0.85

Calls 6

RemoveLeadingWhitespaceFunction · 0.85
RemoveTrailingWhitespaceFunction · 0.85
ConsumePrefixFunction · 0.50
StrCatFunction · 0.50
sizeMethod · 0.45
ClearMethod · 0.45

Tested by 1

CreateNodeDefMethod · 0.68