Skip to content

Commit

Permalink
- adds support for builder getting error mappings
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Biret <vibiret@microsoft.com>
  • Loading branch information
baywet committed Feb 2, 2022
1 parent 52cb48e commit 6971010
Show file tree
Hide file tree
Showing 6 changed files with 1,345 additions and 1,093 deletions.
213 changes: 107 additions & 106 deletions src/Kiota.Builder/CodeDOM/CodeClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,128 +2,129 @@
using System.Collections.Generic;
using System.Linq;

namespace Kiota.Builder
namespace Kiota.Builder;

public enum CodeClassKind {
Custom,
RequestBuilder,
Model,
QueryParameters,
/// <summary>
/// A single parameter to be provided by the SDK user which will contain query parameters, request body, options, etc.
/// Only used for languages that do not support overloads or optional parameters like go.
/// </summary>
ParameterSet,
}
/// <summary>
/// CodeClass represents an instance of a Class to be generated in source code
/// </summary>
public class CodeClass : CodeBlock, IDocumentedElement, ITypeDefinition
{
public enum CodeClassKind {
Custom,
RequestBuilder,
Model,
QueryParameters,
/// <summary>
/// A single parameter to be provided by the SDK user which will contain query parameters, request body, options, etc.
/// Only used for languages that do not support overloads or optional parameters like go.
/// </summary>
ParameterSet,
private string name;
public CodeClass():base()
{
StartBlock = new Declaration() { Parent = this};
EndBlock = new End() { Parent = this };
}
public CodeClassKind ClassKind { get; set; } = CodeClassKind.Custom;

public bool IsErrorDefinition { get; set; }

public string Description {get; set;}
/// <summary>
/// CodeClass represents an instance of a Class to be generated in source code
/// Name of Class
/// </summary>
public class CodeClass : CodeBlock, IDocumentedElement, ITypeDefinition
public override string Name
{
private string name;
public CodeClass():base()
get => name;
set
{
StartBlock = new Declaration() { Parent = this};
EndBlock = new End() { Parent = this };
}
public CodeClassKind ClassKind { get; set; } = CodeClassKind.Custom;

public string Description {get; set;}
/// <summary>
/// Name of Class
/// </summary>
public override string Name
{
get => name;
set
{
name = value;
StartBlock.Name = name;
}
name = value;
StartBlock.Name = name;
}
}

public void SetIndexer(CodeIndexer indexer)
{
if(indexer == null)
throw new ArgumentNullException(nameof(indexer));
if(InnerChildElements.Values.OfType<CodeIndexer>().Any())
throw new InvalidOperationException("this class already has an indexer, remove it first");
AddRange(indexer);
}
public void SetIndexer(CodeIndexer indexer)
{
if(indexer == null)
throw new ArgumentNullException(nameof(indexer));
if(InnerChildElements.Values.OfType<CodeIndexer>().Any())
throw new InvalidOperationException("this class already has an indexer, remove it first");
AddRange(indexer);
}

public IEnumerable<CodeProperty> AddProperty(params CodeProperty[] properties)
{
if(properties == null || properties.Any(x => x == null))
throw new ArgumentNullException(nameof(properties));
if(!properties.Any())
throw new ArgumentOutOfRangeException(nameof(properties));
return AddRange(properties);
}
public CodeProperty GetPropertyOfKind(CodePropertyKind kind) =>
Properties.FirstOrDefault(x => x.IsOfKind(kind));
public IEnumerable<CodeProperty> Properties => InnerChildElements.Values.OfType<CodeProperty>();
public IEnumerable<CodeMethod> Methods => InnerChildElements.Values.OfType<CodeMethod>();
public IEnumerable<CodeProperty> AddProperty(params CodeProperty[] properties)
{
if(properties == null || properties.Any(x => x == null))
throw new ArgumentNullException(nameof(properties));
if(!properties.Any())
throw new ArgumentOutOfRangeException(nameof(properties));
return AddRange(properties);
}
public CodeProperty GetPropertyOfKind(CodePropertyKind kind) =>
Properties.FirstOrDefault(x => x.IsOfKind(kind));
public IEnumerable<CodeProperty> Properties => InnerChildElements.Values.OfType<CodeProperty>();
public IEnumerable<CodeMethod> Methods => InnerChildElements.Values.OfType<CodeMethod>();

public bool ContainsMember(string name)
{
return InnerChildElements.ContainsKey(name);
}
public bool ContainsMember(string name)
{
return InnerChildElements.ContainsKey(name);
}

public IEnumerable<CodeMethod> AddMethod(params CodeMethod[] methods)
{
if(methods == null || methods.Any(x => x == null))
throw new ArgumentNullException(nameof(methods));
if(!methods.Any())
throw new ArgumentOutOfRangeException(nameof(methods));
return AddRange(methods);
}
public IEnumerable<CodeMethod> AddMethod(params CodeMethod[] methods)
{
if(methods == null || methods.Any(x => x == null))
throw new ArgumentNullException(nameof(methods));
if(!methods.Any())
throw new ArgumentOutOfRangeException(nameof(methods));
return AddRange(methods);
}

public IEnumerable<CodeClass> AddInnerClass(params CodeClass[] codeClasses)
{
if(codeClasses == null || codeClasses.Any(x => x == null))
throw new ArgumentNullException(nameof(codeClasses));
if(!codeClasses.Any())
throw new ArgumentOutOfRangeException(nameof(codeClasses));
return AddRange(codeClasses);
}
public CodeClass GetParentClass() {
if(StartBlock is Declaration declaration)
return declaration.Inherits?.TypeDefinition as CodeClass;
else return null;
}
public CodeClass GetGreatestGrandparent(CodeClass startClassToSkip = null) {
var parentClass = GetParentClass();
if(parentClass == null)
return startClassToSkip != null && startClassToSkip == this ? null : this;
// we don't want to return the current class if this is the start node in the inheritance tree and doesn't have parent
else
return parentClass.GetGreatestGrandparent(startClassToSkip);
}
public IEnumerable<CodeClass> AddInnerClass(params CodeClass[] codeClasses)
{
if(codeClasses == null || codeClasses.Any(x => x == null))
throw new ArgumentNullException(nameof(codeClasses));
if(!codeClasses.Any())
throw new ArgumentOutOfRangeException(nameof(codeClasses));
return AddRange(codeClasses);
}
public CodeClass GetParentClass() {
if(StartBlock is Declaration declaration)
return declaration.Inherits?.TypeDefinition as CodeClass;
else return null;
}

public CodeClass GetGreatestGrandparent(CodeClass startClassToSkip = null) {
var parentClass = GetParentClass();
if(parentClass == null)
return startClassToSkip != null && startClassToSkip == this ? null : this;
// we don't want to return the current class if this is the start node in the inheritance tree and doesn't have parent
else
return parentClass.GetGreatestGrandparent(startClassToSkip);
}

public bool IsOfKind(params CodeClassKind[] kinds) {
return kinds?.Contains(ClassKind) ?? false;
}
public bool IsOfKind(params CodeClassKind[] kinds) {
return kinds?.Contains(ClassKind) ?? false;
}

public class Declaration : BlockDeclaration
{
private CodeType inherits;
public CodeType Inherits { get => inherits; set {
EnsureElementsAreChildren(value);
inherits = value;
} }
private readonly List<CodeType> implements = new ();
public void AddImplements(params CodeType[] types) {
if(types == null || types.Any(x => x == null))
throw new ArgumentNullException(nameof(types));
EnsureElementsAreChildren(types);
implements.AddRange(types);
}
public IEnumerable<CodeType> Implements => implements;
{
private CodeType inherits;
public CodeType Inherits { get => inherits; set {
EnsureElementsAreChildren(value);
inherits = value;
} }
private readonly List<CodeType> implements = new ();
public void AddImplements(params CodeType[] types) {
if(types == null || types.Any(x => x == null))
throw new ArgumentNullException(nameof(types));
EnsureElementsAreChildren(types);
implements.AddRange(types);
}
public IEnumerable<CodeType> Implements => implements;
}

public class End : BlockEnd
{
}
public class End : BlockEnd
{
}
}
Loading

0 comments on commit 6971010

Please sign in to comment.