-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6d558e9
commit ec604a7
Showing
52 changed files
with
2,116 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
using System.Text; | ||
using Microsoft.CodeAnalysis.Text; | ||
|
||
namespace Altinn.App.Analyzers; | ||
|
||
public abstract record ApplicationMetadataResult | ||
{ | ||
private ApplicationMetadataResult() { } | ||
|
||
public sealed record Content(ApplicationMetadata Value, SourceText SourceText, string FilePath) | ||
: ApplicationMetadataResult; | ||
|
||
public sealed record FileNotFound(string FilePath) : ApplicationMetadataResult; | ||
|
||
public sealed record CouldNotReadFile(Exception Exception, string FilePath) : ApplicationMetadataResult; | ||
|
||
public sealed record CouldNotParse(Exception Exception, SourceText SourceText, string FilePath) | ||
: ApplicationMetadataResult; | ||
} | ||
|
||
public static class ApplicationMetadataFileReader | ||
{ | ||
public static readonly string ApplicationMetadataFilePath = Path.Combine("config", "applicationmetadata.json"); | ||
|
||
public static ApplicationMetadataResult Read(in MetadataAnalyzerContext context) | ||
{ | ||
var file = Path.Combine(context.ProjectDir, ApplicationMetadataFilePath); | ||
|
||
SourceText sourceText; | ||
string jsonText; | ||
try | ||
{ | ||
context.OnApplicationMetadataReadBefore?.Invoke(); | ||
|
||
if (!File.Exists(file)) | ||
return new ApplicationMetadataResult.FileNotFound(file); | ||
|
||
jsonText = File.ReadAllText(file, Encoding.UTF8); | ||
sourceText = SourceText.From(jsonText, Encoding.UTF8); | ||
} | ||
catch (Exception ex) | ||
{ | ||
return new ApplicationMetadataResult.CouldNotReadFile(ex, file); | ||
} | ||
Check notice Code scanning / CodeQL Generic catch clause Note
Generic catch clause.
|
||
|
||
ApplicationMetadata metadata; | ||
try | ||
{ | ||
context.OnApplicationMetadataDeserializationBefore?.Invoke(); | ||
|
||
metadata = ApplicationMetadataJsonReader.Read(jsonText); | ||
} | ||
catch (Exception ex) | ||
{ | ||
return new ApplicationMetadataResult.CouldNotParse(ex, sourceText, file); | ||
} | ||
Check notice Code scanning / CodeQL Generic catch clause Note
Generic catch clause.
|
||
|
||
return new ApplicationMetadataResult.Content(metadata, sourceText, file); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Linq; | ||
|
||
namespace Altinn.App.Analyzers; | ||
|
||
public readonly record struct ParsedJsonValue<T>(T Value, IJsonLineInfo LineInfo); | ||
|
||
public sealed record ApplicationMetadata(ParsedJsonValue<IReadOnlyList<DataTypeInfo>> DataTypes); | ||
|
||
public sealed record DataTypeInfo(ParsedJsonValue<string> Id, ParsedJsonValue<AppLogicInfo>? AppLogic); | ||
|
||
public sealed record AppLogicInfo(ParsedJsonValue<string> ClassRef); | ||
|
||
public static class ApplicationMetadataJsonReader | ||
{ | ||
public static ApplicationMetadata Read(string json) | ||
{ | ||
var dataTypeInfo = new List<DataTypeInfo>(); | ||
|
||
using var reader = new JsonTextReader(new StringReader(json)); | ||
|
||
JsonLoadSettings loadSettings = new JsonLoadSettings() | ||
{ | ||
CommentHandling = CommentHandling.Ignore, | ||
LineInfoHandling = LineInfoHandling.Load | ||
}; | ||
|
||
var metadata = JObject.Load(reader, loadSettings); | ||
var dataTypesArr = metadata.GetValue("dataTypes") as JArray; | ||
if (dataTypesArr is null) | ||
throw new JsonException("Failed to parse 'dataTypes'"); | ||
var dataTypes = dataTypesArr.ReadWithLineInfo((IReadOnlyList<DataTypeInfo>)dataTypeInfo, "dataTypes"); | ||
|
||
foreach (var dataTypeToken in dataTypesArr) | ||
{ | ||
var dataType = (dataTypeToken as JObject) ?? throw new JsonException("Failed to parse 'dataType'"); | ||
|
||
var dataTypeId = dataType.GetStringValue("id"); | ||
|
||
var appLogicObj = dataType.GetValue("appLogic") as JObject; | ||
if (appLogicObj is null) | ||
{ | ||
dataTypeInfo.Add(new DataTypeInfo(dataTypeId, AppLogic: null)); | ||
continue; | ||
} | ||
|
||
var classRef = appLogicObj.GetStringValue("classRef"); | ||
var appLogic = appLogicObj.ReadWithLineInfo(new AppLogicInfo(classRef), "appLogic"); | ||
dataTypeInfo.Add(new DataTypeInfo(dataTypeId, appLogic)); | ||
} | ||
|
||
return new ApplicationMetadata(dataTypes); | ||
} | ||
|
||
private static ParsedJsonValue<string> GetStringValue(this JObject obj, string propertyName) | ||
{ | ||
var token = obj.GetValue(propertyName) ?? throw new JsonException($"Failed to parse '{propertyName}'"); | ||
var jvalue = (token as JValue) ?? throw new JsonException($"Failed to parse '{propertyName}'"); | ||
var value = (jvalue.Value as string) ?? throw new JsonException($"Failed to parse '{propertyName}'"); | ||
|
||
return jvalue.ReadWithLineInfo(value, propertyName); | ||
} | ||
|
||
private static ParsedJsonValue<T> ReadWithLineInfo<T>(this JToken token, T value, string propertyName) | ||
{ | ||
if (token is not IJsonLineInfo lineInfo || !lineInfo.HasLineInfo()) | ||
throw new JsonException($"Could not read line info while parsing '{propertyName}'"); | ||
|
||
return new ParsedJsonValue<T>(value, lineInfo); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.