| OLD | NEW |
| 1 #region Copyright notice and license | 1 #region Copyright notice and license |
| 2 // Protocol Buffers - Google's data interchange format | 2 // Protocol Buffers - Google's data interchange format |
| 3 // Copyright 2016 Google Inc. All rights reserved. | 3 // Copyright 2016 Google Inc. All rights reserved. |
| 4 // https://developers.google.com/protocol-buffers/ | 4 // https://developers.google.com/protocol-buffers/ |
| 5 // | 5 // |
| 6 // Redistribution and use in source and binary forms, with or without | 6 // Redistribution and use in source and binary forms, with or without |
| 7 // modification, are permitted provided that the following conditions are | 7 // modification, are permitted provided that the following conditions are |
| 8 // met: | 8 // met: |
| 9 // | 9 // |
| 10 // * Redistributions of source code must retain the above copyright | 10 // * Redistributions of source code must retain the above copyright |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 45 /// <summary> | 45 /// <summary> |
| 46 /// Converts a timestamp specified in seconds/nanoseconds to a string. | 46 /// Converts a timestamp specified in seconds/nanoseconds to a string. |
| 47 /// </summary> | 47 /// </summary> |
| 48 /// <remarks> | 48 /// <remarks> |
| 49 /// If the value is a normalized duration in the range described in <c>f
ield_mask.proto</c>, | 49 /// If the value is a normalized duration in the range described in <c>f
ield_mask.proto</c>, |
| 50 /// <paramref name="diagnosticOnly"/> is ignored. Otherwise, if the para
meter is <c>true</c>, | 50 /// <paramref name="diagnosticOnly"/> is ignored. Otherwise, if the para
meter is <c>true</c>, |
| 51 /// a JSON object with a warning is returned; if it is <c>false</c>, an
<see cref="InvalidOperationException"/> is thrown. | 51 /// a JSON object with a warning is returned; if it is <c>false</c>, an
<see cref="InvalidOperationException"/> is thrown. |
| 52 /// </remarks> | 52 /// </remarks> |
| 53 /// <param name="paths">Paths in the field mask</param> | 53 /// <param name="paths">Paths in the field mask</param> |
| 54 /// <param name="diagnosticOnly">Determines the handling of non-normaliz
ed values</param> | 54 /// <param name="diagnosticOnly">Determines the handling of non-normaliz
ed values</param> |
| 55 /// <exception cref="InvalidOperationException">The represented field ma
sk is invalid, and <paramref name="diagnosticOnly"/> is <c>false</c>.</exception
> | 55 /// <exception cref="InvalidOperationException">The represented duration
is invalid, and <paramref name="diagnosticOnly"/> is <c>false</c>.</exception> |
| 56 internal static string ToJson(IList<string> paths, bool diagnosticOnly) | 56 internal static string ToJson(IList<string> paths, bool diagnosticOnly) |
| 57 { | 57 { |
| 58 var firstInvalid = paths.FirstOrDefault(p => !ValidatePath(p)); | 58 var firstInvalid = paths.FirstOrDefault(p => !ValidatePath(p)); |
| 59 if (firstInvalid == null) | 59 if (firstInvalid == null) |
| 60 { | 60 { |
| 61 var writer = new StringWriter(); | 61 var writer = new StringWriter(); |
| 62 #if DOTNET35 | 62 JsonFormatter.WriteString(writer, string.Join(",", paths.Select(
JsonFormatter.ToCamelCase))); |
| 63 var query = paths.Select(JsonFormatter.ToJsonName); | |
| 64 JsonFormatter.WriteString(writer, string.Join(",", query.ToArray
())); | |
| 65 #else | |
| 66 JsonFormatter.WriteString(writer, string.Join(",", paths.Select(
JsonFormatter.ToJsonName))); | |
| 67 #endif | |
| 68 return writer.ToString(); | 63 return writer.ToString(); |
| 69 } | 64 } |
| 70 else | 65 else |
| 71 { | 66 { |
| 72 if (diagnosticOnly) | 67 if (diagnosticOnly) |
| 73 { | 68 { |
| 74 var writer = new StringWriter(); | 69 var writer = new StringWriter(); |
| 75 writer.Write("{ \"@warning\": \"Invalid FieldMask\", \"paths
\": "); | 70 writer.Write("{ \"@warning\": \"Invalid FieldMask\", \"paths
\": "); |
| 76 JsonFormatter.Default.WriteList(writer, (IList)paths); | 71 JsonFormatter.Default.WriteList(writer, (IList)paths); |
| 77 writer.Write(" }"); | 72 writer.Write(" }"); |
| 78 return writer.ToString(); | 73 return writer.ToString(); |
| 79 } | 74 } |
| 80 else | 75 else |
| 81 { | 76 { |
| 82 throw new InvalidOperationException($"Invalid field mask to
be converted to JSON: {firstInvalid}"); | 77 throw new InvalidOperationException($"Invalid field mask to
be converted to JSON: {firstInvalid}"); |
| 83 } | 78 } |
| 84 } | 79 } |
| 85 } | 80 } |
| 86 | 81 |
| 87 /// <summary> | 82 /// <summary> |
| 88 /// Checks whether the given path is valid for a field mask. | 83 /// Camel-case converter with added strictness for field mask formatting
. |
| 89 /// </summary> | 84 /// </summary> |
| 90 /// <returns>true if the path is valid; false otherwise</returns> | 85 /// <exception cref="InvalidOperationException">The field mask is invali
d for JSON representation</exception> |
| 91 private static bool ValidatePath(string input) | 86 private static bool ValidatePath(string input) |
| 92 { | 87 { |
| 93 for (int i = 0; i < input.Length; i++) | 88 for (int i = 0; i < input.Length; i++) |
| 94 { | 89 { |
| 95 char c = input[i]; | 90 char c = input[i]; |
| 96 if (c >= 'A' && c <= 'Z') | 91 if (c >= 'A' && c <= 'Z') |
| 97 { | 92 { |
| 98 return false; | 93 return false; |
| 99 } | 94 } |
| 100 if (c == '_' && i < input.Length - 1) | 95 if (c == '_' && i < input.Length - 1) |
| (...skipping 18 matching lines...) Expand all Loading... |
| 119 /// diagnose problems - the regular JSON formatter will still throw an e
xception for non-normalized | 114 /// diagnose problems - the regular JSON formatter will still throw an e
xception for non-normalized |
| 120 /// values. | 115 /// values. |
| 121 /// </remarks> | 116 /// </remarks> |
| 122 /// <returns>A string representation of this value.</returns> | 117 /// <returns>A string representation of this value.</returns> |
| 123 public string ToDiagnosticString() | 118 public string ToDiagnosticString() |
| 124 { | 119 { |
| 125 return ToJson(Paths, true); | 120 return ToJson(Paths, true); |
| 126 } | 121 } |
| 127 } | 122 } |
| 128 } | 123 } |
| OLD | NEW |