api(dotnet): embrace nullable references (#6964)

This commit is contained in:
Pavel Feldman 2021-06-08 12:20:35 -07:00 committed by GitHub
parent cc2a7ef369
commit 6ec70bc0ac
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 14 deletions

View file

@ -37,7 +37,6 @@ const documentedResults = new Map(); // will hold documentation for new types
const enumTypes = new Map(); const enumTypes = new Map();
/** @type {Map<string, Documentation.Type>} */ /** @type {Map<string, Documentation.Type>} */
const optionTypes = new Map(); const optionTypes = new Map();
const nullableTypes = ['int', 'bool', 'decimal', 'float'];
const customTypeNames = new Map([ const customTypeNames = new Map([
['domcontentloaded', 'DOMContentLoaded'], ['domcontentloaded', 'DOMContentLoaded'],
['networkidle', 'NetworkIdle'], ['networkidle', 'NetworkIdle'],
@ -355,13 +354,14 @@ function renderMember(member, parent, options, out) {
if (member.spec) if (member.spec)
out.push(...XmlDoc.renderXmlDoc(member.spec, maxDocumentationColumnWidth)); out.push(...XmlDoc.renderXmlDoc(member.spec, maxDocumentationColumnWidth));
if (!member.clazz) if (!member.clazz)
out.push(`[JsonPropertyName("${jsonName}")]`) out.push(`${member.required ? '[Required]\n' : ''}[JsonPropertyName("${jsonName}")]`)
if (!type.endsWith('?') && !member.required && nullableTypes.includes(type)) if (!type.endsWith('?') && !member.required)
type = `${type}?`; type = `${type}?`;
const requiredSuffix = type.endsWith('?') ? '' : ' = default!;';
if (member.clazz) if (member.clazz)
out.push(`public ${type} ${name} { get; }`); out.push(`public ${type} ${name} { get; }`);
else else
out.push(`public ${type} ${name} { get; set; }`); out.push(`public ${type} ${name} { get; set; }${requiredSuffix}`);
} }
return; return;
} }
@ -586,8 +586,7 @@ function renderMethod(member, parent, name, options, out) {
function pushArg(innerArgType, innerArgName, argument, isExploded = false) { function pushArg(innerArgType, innerArgName, argument, isExploded = false) {
if (innerArgType === 'null') if (innerArgType === 'null')
return; return;
const isNullable = nullableTypes.includes(innerArgType); const requiredPrefix = (argument.required || isExploded) ? "" : "?";
const requiredPrefix = (argument.required || isExploded) ? "" : isNullable ? "?" : "";
const requiredSuffix = (argument.required || isExploded) ? "" : " = default"; const requiredSuffix = (argument.required || isExploded) ? "" : " = default";
var push = `${innerArgType}${requiredPrefix} ${innerArgName}${requiredSuffix}`; var push = `${innerArgType}${requiredPrefix} ${innerArgName}${requiredSuffix}`;
if (isExploded) if (isExploded)
@ -608,8 +607,8 @@ function renderMethod(member, parent, name, options, out) {
if (options.mode === 'options' || options.mode === 'base') { if (options.mode === 'options' || options.mode === 'base') {
const optionsType = member.clazz.name + name + 'Options'; const optionsType = member.clazz.name + name + 'Options';
optionTypes.set(optionsType, arg.type); optionTypes.set(optionsType, arg.type);
args.push(`${optionsType} options = default`); args.push(`${optionsType}? options = default`);
argTypeMap.set(`${optionsType} options = default`, 'options'); argTypeMap.set(`${optionsType}? options = default`, 'options');
addParamsDoc('options', ['Call options']); addParamsDoc('options', ['Call options']);
} else { } else {
arg.type.properties.forEach(processArg); arg.type.properties.forEach(processArg);
@ -619,8 +618,8 @@ function renderMethod(member, parent, name, options, out) {
if (arg.type.expression === '[string]|[path]') { if (arg.type.expression === '[string]|[path]') {
let argName = toArgumentName(arg.name); let argName = toArgumentName(arg.name);
pushArg("string", `${argName} = null`, arg); pushArg("string?", `${argName} = default`, arg);
pushArg("string", `${argName}Path = null`, arg); pushArg("string?", `${argName}Path = default`, arg);
if (arg.spec) { if (arg.spec) {
addParamsDoc(argName, XmlDoc.renderTextOnly(arg.spec, maxDocumentationColumnWidth)); addParamsDoc(argName, XmlDoc.renderTextOnly(arg.spec, maxDocumentationColumnWidth));
addParamsDoc(`${argName}Path`, [`Instead of specifying <paramref name="${argName}"/>, gives the file name to load from.`]); addParamsDoc(`${argName}Path`, [`Instead of specifying <paramref name="${argName}"/>, gives the file name to load from.`]);
@ -794,7 +793,6 @@ function translateType(type, parent, generateNameCallback = t => t.name, optiona
// Regular primitive enums are named in the markdown. // Regular primitive enums are named in the markdown.
if (type.name) { if (type.name) {
enumTypes.set(type.name, type.union.map(t => t.name)); enumTypes.set(type.name, type.union.map(t => t.name));
nullableTypes.push(type.name);
return optional ? type.name + '?' : type.name; return optional ? type.name + '?' : type.name;
} }
return null; return null;
@ -832,7 +830,7 @@ function translateType(type, parent, generateNameCallback = t => t.name, optiona
} else if (type.name === 'Object') { } else if (type.name === 'Object') {
registerModelType(objectName, type); registerModelType(objectName, type);
} }
return objectName; return `${objectName}${optional ? '?' : ''}`;
} }
if (type.name === 'Map') { if (type.name === 'Map') {
@ -881,7 +879,7 @@ function translateType(type, parent, generateNameCallback = t => t.name, optiona
// there's a chance this is a name we've already seen before, so check // there's a chance this is a name we've already seen before, so check
// this is also where we map known types, like boolean -> bool, etc. // this is also where we map known types, like boolean -> bool, etc.
let name = classNameMap.get(type.name) || type.name; let name = classNameMap.get(type.name) || type.name;
return `${name}`; return `${name}${optional ? '?' : ''}`;
} }
/** /**

View file

@ -24,6 +24,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Drawing; using System.Drawing;
using System.Globalization; using System.Globalization;
using System.IO; using System.IO;
@ -34,7 +35,11 @@ using System.Text.RegularExpressions;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
#nullable enable
namespace Microsoft.Playwright namespace Microsoft.Playwright
{ {
[CONTENT] [CONTENT]
} }
#nullable disable