@@ -8,10 +8,18 @@ namespace DotNet.Testcontainers.Images
8
8
[ PublicAPI ]
9
9
public sealed class DockerImage : IImage
10
10
{
11
+ private const string LatestTag = "latest" ;
12
+
11
13
private static readonly Func < string , IImage > GetDockerImage = MatchImage . Match ;
12
14
15
+ private static readonly char [ ] TrimChars = { ' ' , ':' , '/' } ;
16
+
13
17
private readonly string _hubImageNamePrefix ;
14
18
19
+ private readonly Lazy < string > _lazyFullName ;
20
+
21
+ private readonly Lazy < string > _lazyHostname ;
22
+
15
23
/// <summary>
16
24
/// Initializes a new instance of the <see cref="DockerImage" /> class.
17
25
/// </summary>
@@ -44,7 +52,7 @@ public DockerImage(string image)
44
52
public DockerImage (
45
53
string repository ,
46
54
string name ,
47
- string tag ,
55
+ string tag = null ,
48
56
string hubImageNamePrefix = null )
49
57
{
50
58
_ = Guard . Argument ( repository , nameof ( repository ) )
@@ -56,11 +64,38 @@ public DockerImage(
56
64
. NotEmpty ( )
57
65
. NotUppercase ( ) ;
58
66
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 ( ) ;
60
89
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
+ } ) ;
64
99
}
65
100
66
101
/// <inheritdoc />
@@ -73,23 +108,14 @@ public DockerImage(
73
108
public string Tag { get ; }
74
109
75
110
/// <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 ;
87
112
88
113
/// <inheritdoc />
89
- public string GetHostname ( )
114
+ public string GetHostname ( ) => _lazyHostname . Value ;
115
+
116
+ private static string TrimOrDefault ( string value , string defaultValue = default )
90
117
{
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 ) ;
93
119
}
94
120
}
95
121
}
0 commit comments