-
Notifications
You must be signed in to change notification settings - Fork 5
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
Conversation
cd0a88e
to
49d3aba
Compare
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 ); |
There was a problem hiding this comment.
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
.
parser.WriteFile( SharedCredentialsFile.DefaultConfigFilePath, defaultConfigFile ); | ||
parser.WriteFile( awsConfigFilePath, awsConfig, Utf8 ); |
There was a problem hiding this comment.
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 🤢
string sectionName = $"profile {profile}"; | ||
if( !data.Sections.ContainsSection( sectionName ) ) { | ||
data.Sections.AddSection( sectionName ); | ||
Debug.Assert( awsConfig is not null ); |
There was a problem hiding this comment.
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 ?
There was a problem hiding this comment.
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 blockbar
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.
There was a problem hiding this comment.
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" );
}
follow up to this comment: #453 (comment)