@@ -8,10 +8,18 @@ namespace DotNet.Testcontainers.Images
88 [ PublicAPI ]
99 public sealed class DockerImage : IImage
1010 {
11+ private const string LatestTag = "latest" ;
12+
1113 private static readonly Func < string , IImage > GetDockerImage = MatchImage . Match ;
1214
15+ private static readonly char [ ] TrimChars = { ' ' , ':' , '/' } ;
16+
1317 private readonly string _hubImageNamePrefix ;
1418
19+ private readonly Lazy < string > _lazyFullName ;
20+
21+ private readonly Lazy < string > _lazyHostname ;
22+
1523 /// <summary>
1624 /// Initializes a new instance of the <see cref="DockerImage" /> class.
1725 /// </summary>
@@ -44,7 +52,7 @@ public DockerImage(string image)
4452 public DockerImage (
4553 string repository ,
4654 string name ,
47- string tag ,
55+ string tag = null ,
4856 string hubImageNamePrefix = null )
4957 {
5058 _ = Guard . Argument ( repository , nameof ( repository ) )
@@ -56,11 +64,38 @@ public DockerImage(
5664 . NotEmpty ( )
5765 . NotUppercase ( ) ;
5866
59- _hubImageNamePrefix = hubImageNamePrefix ;
67+ _hubImageNamePrefix = TrimOrDefault ( hubImageNamePrefix ) ;
68+
69+ Repository = TrimOrDefault ( repository , repository ) ;
70+ Name = TrimOrDefault ( name , name ) ;
71+ Tag = TrimOrDefault ( tag , LatestTag ) ;
72+
73+ _lazyFullName = new Lazy < string > ( ( ) =>
74+ {
75+ var imageComponents = new [ ] { _hubImageNamePrefix , Repository , Name }
76+ . Where ( imageComponent => ! string . IsNullOrEmpty ( imageComponent ) ) ;
77+
78+ return string . Join ( "/" , imageComponents ) + ":" + Tag ;
79+ } ) ;
80+
81+ _lazyHostname = new Lazy < string > ( ( ) =>
82+ {
83+ var firstSegmentOfRepository = new [ ] { _hubImageNamePrefix , Repository }
84+ . Where ( imageComponent => ! string . IsNullOrEmpty ( imageComponent ) )
85+ . DefaultIfEmpty ( string . Empty )
86+ . First ( )
87+ . Split ( '/' )
88+ . First ( ) ;
6089
61- Repository = repository ;
62- Name = name ;
63- Tag = string . IsNullOrEmpty ( tag ) ? "latest" : tag ;
90+ if ( firstSegmentOfRepository . IndexOfAny ( new [ ] { '.' , ':' } ) >= 0 )
91+ {
92+ return firstSegmentOfRepository ;
93+ }
94+ else
95+ {
96+ return null ;
97+ }
98+ } ) ;
6499 }
65100
66101 /// <inheritdoc />
@@ -73,23 +108,14 @@ public DockerImage(
73108 public string Tag { get ; }
74109
75110 /// <inheritdoc />
76- public string FullName
77- {
78- get
79- {
80- var imageComponents = new [ ] { _hubImageNamePrefix , Repository , Name }
81- . Where ( imageComponent => ! string . IsNullOrEmpty ( imageComponent ) )
82- . Select ( imageComponent => imageComponent . Trim ( '/' , ':' ) )
83- . Where ( imageComponent => ! string . IsNullOrEmpty ( imageComponent ) ) ;
84- return string . Join ( "/" , imageComponents ) + ":" + Tag ;
85- }
86- }
111+ public string FullName => _lazyFullName . Value ;
87112
88113 /// <inheritdoc />
89- public string GetHostname ( )
114+ public string GetHostname ( ) => _lazyHostname . Value ;
115+
116+ private static string TrimOrDefault ( string value , string defaultValue = default )
90117 {
91- var firstSegmentOfRepository = ( string . IsNullOrEmpty ( _hubImageNamePrefix ) ? Repository : _hubImageNamePrefix ) . Split ( '/' ) [ 0 ] ;
92- return firstSegmentOfRepository . IndexOfAny ( new [ ] { '.' , ':' } ) >= 0 ? firstSegmentOfRepository : null ;
118+ return string . IsNullOrEmpty ( value ) ? defaultValue : value . Trim ( TrimChars ) ;
93119 }
94120 }
95121}
0 commit comments