Skip to content

Commit

Permalink
Generate required properties as non-nullable
Browse files Browse the repository at this point in the history
  • Loading branch information
alnkesq committed Jan 14, 2025
1 parent 822196d commit 4dc73a0
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
6 changes: 6 additions & 0 deletions src/FishyFlip/OpenGraphParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,12 @@ public void Dispose()
urlEmbed = tag1;
}

// These three fields are all required, according to the lexicon.
if (urlEmbed == null || title == null || description == null)
{
return null;
}

var external = new External(urlEmbed, title, description, image);
return new EmbedExternal(external);
}
Expand Down
3 changes: 2 additions & 1 deletion tools/FFSourceGen/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2252,7 +2252,8 @@ private void GenerateHeader(StringBuilder sb)
sb.AppendLine();

// Add #nullable
sb.AppendLine("#nullable enable");
sb.AppendLine("#nullable enable annotations");
sb.AppendLine("#nullable disable warnings");
sb.AppendLine();
}

Expand Down
11 changes: 8 additions & 3 deletions tools/FFSourceGen/PropertyGeneration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ private void SetNamespace(string ns, string cns)

public bool IsDefinitionFile => this.Document.Id.EndsWith("defs");

public string Type => this.GetPropertyType(this.ClassName, this.ClassName, this.PropertyDefinition);
public string Type => this.GetPropertyType(this.ClassName, this.ClassName, this.PropertyDefinition, this.Class, this.Key);

public bool IsBaseType => this._IsBaseType(this.Type);

Expand Down Expand Up @@ -318,7 +318,7 @@ private string GetUnknownObjectType(PropertyDefinition property)
return "ATObject";
}

private string GetPropertyType(string className, string name, PropertyDefinition property)
private string GetPropertyType(string className, string name, PropertyDefinition property, ClassGeneration? ownerClass = null, string? jsonName = null)
{
var baseType = property.Type?.ToLower() switch
{
Expand Down Expand Up @@ -350,7 +350,12 @@ private string GetPropertyType(string className, string name, PropertyDefinition
throw new InvalidOperationException("Base Type is null or empty.");
}

return $"{baseType}?";
if (ownerClass?.Definition?.Required.Contains(jsonName!) == true && baseType != "DateTime")
{
return baseType;
}

return property.Required ? baseType : $"{baseType}?";
}

private string GetObjectType(PropertyDefinition property)
Expand Down

0 comments on commit 4dc73a0

Please sign in to comment.