/// <summary>
 /// Initializes a new instance of the <see cref="SmartAttribute"/> class.
 /// </summary>
 /// <param name="identifier">The SMART identifier of the attribute.</param>
 /// <param name="name">The name of the attribute.</param>
 /// <param name="rawValueConversion">A delegate for converting the raw byte 
 /// array into a value (or null to use the attribute value).</param>
 /// <param name="sensorType">Type of the sensor or null if no sensor is to 
 /// be created.</param>
 /// <param name="sensorChannel">If there exists more than one attribute with 
 /// the same sensor channel and type, then a sensor is created only for the  
 /// first attribute.</param>
 /// <param name="defaultHiddenSensor">True to hide the sensor initially.</param>
 /// <param name="parameterDescriptions">Description for the parameters of the sensor 
 /// (or null).</param>
 public SmartAttribute(byte identifier, string name,
   RawValueConversion rawValueConversion, SensorType? sensorType, 
   int sensorChannel, bool defaultHiddenSensor = false,
   ParameterDescription[] parameterDescriptions = null) 
 {
   this.Identifier = identifier;
   this.Name = name;
   this.rawValueConversion = rawValueConversion;
   this.SensorType = sensorType;
   this.SensorChannel = sensorChannel;
   this.DefaultHiddenSensor = defaultHiddenSensor;
   this.ParameterDescriptions = parameterDescriptions;
 }
        private ModelDescription GenerateComplexTypeModelDescription(Type modelType)
        {
            ComplexTypeModelDescription complexModelDescription = new ComplexTypeModelDescription
            {
                Name = ModelNameHelper.GetModelName(modelType),
                ModelType = modelType,
                Documentation = CreateDefaultDocumentation(modelType)
            };

            GeneratedModels.Add(complexModelDescription.Name, complexModelDescription);
            bool hasDataContractAttribute = modelType.GetCustomAttribute<DataContractAttribute>() != null;
            PropertyInfo[] properties = modelType.GetProperties(BindingFlags.Public | BindingFlags.Instance);
            foreach (PropertyInfo property in properties)
            {
                if (ShouldDisplayMember(property, hasDataContractAttribute))
                {
                    ParameterDescription propertyModel = new ParameterDescription
                    {
                        Name = GetMemberName(property, hasDataContractAttribute)
                    };

                    if (DocumentationProvider != null)
                    {
                        propertyModel.Documentation = DocumentationProvider.GetDocumentation(property);
                    }

                    GenerateAnnotations(property, propertyModel);
                    complexModelDescription.Properties.Add(propertyModel);
                    propertyModel.TypeDescription = GetOrCreateModelDescription(property.PropertyType);
                }
            }

            FieldInfo[] fields = modelType.GetFields(BindingFlags.Public | BindingFlags.Instance);
            foreach (FieldInfo field in fields)
            {
                if (ShouldDisplayMember(field, hasDataContractAttribute))
                {
                    ParameterDescription propertyModel = new ParameterDescription
                    {
                        Name = GetMemberName(field, hasDataContractAttribute)
                    };

                    if (DocumentationProvider != null)
                    {
                        propertyModel.Documentation = DocumentationProvider.GetDocumentation(field);
                    }

                    complexModelDescription.Properties.Add(propertyModel);
                    propertyModel.TypeDescription = GetOrCreateModelDescription(field.FieldType);
                }
            }

            return complexModelDescription;
        }
        private void GenerateAnnotations(MemberInfo property, ParameterDescription propertyModel)
        {
            List<ParameterAnnotation> annotations = new List<ParameterAnnotation>();

            IEnumerable<Attribute> attributes = property.GetCustomAttributes();
            foreach (Attribute attribute in attributes)
            {
                Func<object, string> textGenerator;
                if (AnnotationTextGenerator.TryGetValue(attribute.GetType(), out textGenerator))
                {
                    annotations.Add(
                        new ParameterAnnotation
                        {
                            AnnotationAttribute = attribute,
                            Documentation = textGenerator(attribute)
                        });
                }
            }

            // Rearrange the annotations
            annotations.Sort((x, y) =>
            {
                // Special-case RequiredAttribute so that it shows up on top
                if (x.AnnotationAttribute is RequiredAttribute)
                {
                    return -1;
                }
                if (y.AnnotationAttribute is RequiredAttribute)
                {
                    return 1;
                }

                // Sort the rest based on alphabetic order of the documentation
                return String.Compare(x.Documentation, y.Documentation, StringComparison.OrdinalIgnoreCase);
            });

            foreach (ParameterAnnotation annotation in annotations)
            {
                propertyModel.Annotations.Add(annotation);
            }
        }
        private static ParameterDescription AddParameterDescription(HelpPageApiModel apiModel,
            ApiParameterDescription apiParameter, ModelDescription typeDescription)
        {
            ParameterDescription parameterDescription = new ParameterDescription
            {
                Name = apiParameter.Name,
                Documentation = apiParameter.Documentation,
                TypeDescription = typeDescription,
            };

            apiModel.UriParameters.Add(parameterDescription);
            return parameterDescription;
        }
Beispiel #5
0
 /// <summary>Parent node: DECLARE FUNCTION</summary>
 /// <param name="context">PROCEDURE DIVISION</param>
 public override void EnterFunctionProcedureDivision(ProgramClassParser.FunctionProcedureDivisionContext context)
 {
     var header = (ProcedureDivisionHeader)context.ProcedureDivisionHeader().Symbol;
     if (header.UsingParameters != null && header.UsingParameters.Count > 0)
         DiagnosticUtils.AddError(header, "TCRFUN_DECLARATION_NO_USING");//TODO#249
     var declaration = (FunctionDeclarationHeader)CurrentNode.CodeElement;
     foreach(var parameter in declaration.Profile.InputParameters) {
         var paramNode = new ParameterDescription(parameter);
         paramNode.SymbolTable = CurrentNode.SymbolTable;
         CurrentNode.SymbolTable.AddVariable(paramNode);
     }
     foreach(var parameter in declaration.Profile.OutputParameters) {
         var paramNode = new ParameterDescription(parameter);
         paramNode.SymbolTable = CurrentNode.SymbolTable;
         CurrentNode.SymbolTable.AddVariable(paramNode);
     }
     foreach(var parameter in declaration.Profile.InoutParameters) {
         var paramNode = new ParameterDescription(parameter);
         paramNode.SymbolTable = CurrentNode.SymbolTable;
         CurrentNode.SymbolTable.AddVariable(paramNode);
     }
     if (declaration.Profile.ReturningParameter != null) {
         var paramNode = new ParameterDescription(declaration.Profile.ReturningParameter);
         paramNode.SymbolTable = CurrentNode.SymbolTable;
         CurrentNode.SymbolTable.AddVariable(paramNode);
     } else
     if (header.ReturningParameter != null) {
         // we might have a RETURNING parameter to convert, but only if there is neither
         // PICTURE nor TYPE clause for the returning parameter in the function declaration.
         // however, this is as syntax error.
         var pentry = new ParameterDescriptionEntry();
         var data = header.ReturningParameter.StorageArea as DataOrConditionStorageArea;
         if (data != null) pentry.DataName = new SymbolDefinition(data.SymbolReference.NameLiteral, data.SymbolReference.Type);
         // pentry.Picture will remain empty, we can't do anything about it
         pentry.DataType = DataType.Unknown;
         declaration.Profile.ReturningParameter = pentry;
     }
     Enter(new ProcedureDivision(header), context);
 }
Beispiel #6
0
        private static void GenerateUriParameters(HelpPageApiModel apiModel, ModelDescriptionGenerator modelGenerator)
        {
            ApiDescription apiDescription = apiModel.ApiDescription;

            foreach (ApiParameterDescription apiParameter in apiDescription.ParameterDescriptions)
            {
                if (apiParameter.Source == ApiParameterSource.FromUri)
                {
                    HttpParameterDescriptor parameterDescriptor        = apiParameter.ParameterDescriptor;
                    Type                        parameterType          = null;
                    ModelDescription            typeDescription        = null;
                    ComplexTypeModelDescription complexTypeDescription = null;
                    if (parameterDescriptor != null)
                    {
                        parameterType          = parameterDescriptor.ParameterType;
                        typeDescription        = modelGenerator.GetOrCreateModelDescription(parameterType);
                        complexTypeDescription = typeDescription as ComplexTypeModelDescription;
                    }

                    // Example:
                    // [TypeConverter(typeof(PointConverter))]
                    // public class Point
                    // {
                    //     public Point(int x, int y)
                    //     {
                    //         X = x;
                    //         Y = y;
                    //     }
                    //     public int X { get; set; }
                    //     public int Y { get; set; }
                    // }
                    // Class Point is bindable with a TypeConverter, so Point will be added to UriParameters collection.
                    //
                    // public class Point
                    // {
                    //     public int X { get; set; }
                    //     public int Y { get; set; }
                    // }
                    // Regular complex class Point will have properties X and Y added to UriParameters collection.
                    if (complexTypeDescription != null &&
                        !IsBindableWithTypeConverter(parameterType))
                    {
                        foreach (ParameterDescription uriParameter in complexTypeDescription.Properties)
                        {
                            apiModel.UriParameters.Add(uriParameter);
                        }
                    }
                    else if (parameterDescriptor != null)
                    {
                        ParameterDescription uriParameter =
                            AddParameterDescription(apiModel, apiParameter, typeDescription);

                        if (!parameterDescriptor.IsOptional)
                        {
                            uriParameter.Annotations.Add(new ParameterAnnotation()
                            {
                                Documentation = "Required"
                            });
                        }

                        object defaultValue = parameterDescriptor.DefaultValue;
                        if (defaultValue != null)
                        {
                            uriParameter.Annotations.Add(new ParameterAnnotation()
                            {
                                Documentation = "Default value is " + Convert.ToString(defaultValue, CultureInfo.InvariantCulture)
                            });
                        }
                    }
                    else
                    {
                        Debug.Assert(parameterDescriptor == null);

                        // If parameterDescriptor is null, this is an undeclared route parameter which only occurs
                        // when source is FromUri. Ignored in request model and among resource parameters but listed
                        // as a simple string here.
                        ModelDescription modelDescription = modelGenerator.GetOrCreateModelDescription(typeof(string));
                        AddParameterDescription(apiModel, apiParameter, modelDescription);
                    }
                }
            }
        }
        private static void GenerateUriParameters(HelpPageApiModel apiModel, ModelDescriptionGenerator modelGenerator)
        {
            ApiDescription apiDescription = apiModel.ApiDescription;

            foreach (ApiParameterDescription apiParameter in apiDescription.ParameterDescriptions)
            {
                if (apiParameter.Source == ApiParameterSource.FromUri)
                {
                    HttpParameterDescriptor parameterDescriptor        = apiParameter.ParameterDescriptor;
                    Type                        parameterType          = null;
                    ModelDescription            typeDescription        = null;
                    ComplexTypeModelDescription complexTypeDescription = null;
                    if (parameterDescriptor != null)
                    {
                        parameterType          = parameterDescriptor.ParameterType;
                        typeDescription        = modelGenerator.GetOrCreateModelDescription(parameterType);
                        complexTypeDescription = typeDescription as ComplexTypeModelDescription;
                    }

                    if (complexTypeDescription != null)
                    {
                        foreach (ParameterDescription uriParameter in complexTypeDescription.Properties)
                        {
                            apiModel.UriParameters.Add(uriParameter);
                        }
                    }
                    else if (parameterDescriptor != null)
                    {
                        ParameterDescription uriParameter =
                            AddParameterDescription(apiModel, apiParameter, typeDescription);

                        if (!parameterDescriptor.IsOptional)
                        {
                            uriParameter.Annotations.Add(new ParameterAnnotation()
                            {
                                Documentation = "Required"
                            });
                        }

                        object defaultValue = parameterDescriptor.DefaultValue;
                        if (defaultValue != null)
                        {
                            uriParameter.Annotations.Add(new ParameterAnnotation()
                            {
                                Documentation = "Default value is " + Convert.ToString(defaultValue, CultureInfo.InvariantCulture)
                            });
                        }
                    }
                    else
                    {
                        Debug.Assert(parameterDescriptor == null);

                        // If parameterDescriptor is null, this is an undeclared route parameter which only occurs
                        // when source is FromUri. Ignored in request model and among resource parameters but listed
                        // as a simple string here.
                        ModelDescription modelDescription = modelGenerator.GetOrCreateModelDescription(typeof(string));
                        AddParameterDescription(apiModel, apiParameter, modelDescription);
                    }
                }
            }
        }