Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor ini file handling logic for write command #454

Merged
merged 1 commit into from
Jun 24, 2024
Merged

Conversation

cfbao
Copy link
Member

@cfbao cfbao commented Jun 21, 2024

follow up to this comment: #453 (comment)

@github-actions github-actions bot added language/csharp size/M A medium-sized PR. labels Jun 21, 2024
@cfbao cfbao force-pushed the cbao/write-refactor branch from cd0a88e to 49d3aba Compare June 21, 2024 19:01
Comment on lines -67 to +76
var data = parser.ReadFile( output );
string awsConfigFilePath = useCredentialProcess ? output : SharedCredentialsFile.DefaultConfigFilePath;
var awsConfig = GetParsedIniFileOrNull( awsConfigFilePath );

string awsCredentialsFilePath = useCredentialProcess ? SharedCredentialsFile.DefaultFilePath : output;
var awsCredentials = GetParsedIniFileOrNull( awsCredentialsFilePath );
Copy link
Member Author

@cfbao cfbao Jun 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the main change.

My main pet peeve in this area is that output and data could have one of two different meanings, depending on the value of useCredentialProcess. I find this confusing lower down where the logic splits into two branches.

So, here, I changed the variable assignment so that output will no longer be used beyond this point, and we will have distinct names for the two different types of files/data.

As a nice side-effect, this is also the last place where we do any INI file parsing, or have any reference to SharedCredentialsFile.

Comment on lines -99 to +115
parser.WriteFile( SharedCredentialsFile.DefaultConfigFilePath, defaultConfigFile );
parser.WriteFile( awsConfigFilePath, awsConfig, Utf8 );
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: without explicitly specifying the encoding, parser will write file using UTF8-BOM 🤢

@cfbao cfbao marked this pull request as ready for review June 21, 2024 19:11
@cfbao cfbao requested a review from a team as a code owner June 21, 2024 19:11
string sectionName = $"profile {profile}";
if( !data.Sections.ContainsSection( sectionName ) ) {
data.Sections.AddSection( sectionName );
Debug.Assert( awsConfig is not null );
Copy link
Member

@boarnoah boarnoah Jun 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the thought process behind this being an assertion only in debug builds?

EDIT: Also we got BMX_DEBUG, should it just be Assert in all builds that is only tested with BMX_DEBUG = 1 ?

Copy link
Member Author

@cfbao cfbao Jun 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The main goal here is to make the compiler's nullable analysis happy in a clear and intentional way.

awsConfig should not be null here when useCredentialProcess is true (because we'd create the AWS config file above). The compiler cannot infer this, so by default it'd mark awsConfig as "may be null", and issue warnings at L88 and below.

We could use the null-forgiving operator !, but it doesn't make our assumptions and intent clear:

  • Why can we assume awsConfig isn't null in these places?
  • When does this assumption hold?

By using Debug.Assert, we're effectively making clarifying statements like

  • foo isn't null inside this if/else/for/while block
  • bar isn't null once this above statement is executed successfully

We can also avoid sprinkling ! in many different places.


The fact that this is also a runtime assertion in debug builds is more or less a side effect that I don't care that much. I don't think we should make it actually asserts at runtime when BMX_DEBUG is set either.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We've used this technique to document our assumptions in code before, e.g.

It's also used by the .NET runtime libraries, e.g.


These are assumptions that should always hold true. If there's a real non-negligible chance that they could be false, then they should become real runtime assertions like

if ( !assumption ) {
	throw new InvalidOperationException( "bad condition" );
}

@cfbao cfbao merged commit 2bf9bd8 into main Jun 24, 2024
9 checks passed
@cfbao cfbao deleted the cbao/write-refactor branch June 24, 2024 17:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
language/csharp size/M A medium-sized PR.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants