Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within I/O and Streams
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
I/O and Streams
how to pass a parameter to URI stored in YAML file
Bronislav Bronek
Ranch Hand
Posts: 40
posted 3 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Is this possible to achive this result in YAML ?
to set {param} in my code and then load url . I would not to parse all
string
(url) in my code
inside yaml file:
connection: url: jdbc:mysql://localhost:{param}/db poolSize: 5 {param}
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
posted 3 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Where does the param value come from? And what are you going to do with the URL afterwards?
Bronislav Bronek
Ranch Hand
Posts: 40
posted 3 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I would like to do something like this in my code:
set.({param} , 80 )
load yaml file
urlDatabase = getFromFile (url)
then I expect
urlDatabase =
jdbc
:mysql://localhost:80/db
now, I must get url and parse this in my code
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
posted 3 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
What's wrong with parsing the URL?
Bronislav Bronek
Ranch Hand
Posts: 40
posted 3 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Could you please tell me how to do this elegantly ?
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
posted 3 years ago
2
Number of slices to send:
Optional 'thank-you' note:
Send
static String fillParameters(String template, Map<?, ?> arguments) { return Pattern.compile("[{]([^}]*?)[}]").matcher(template).replaceAll( match -> Optional.ofNullable(arguments.get(match.group(1))) .map(Object::toString) .orElseGet(match::group) ); }
var dbConnectionUri = URI.create(fillParameters(dbConfig.url(), Map.of("param", 80)));
Rob Spoor
Sheriff
Posts: 22783
131
I like...
posted 3 years ago
1
Number of slices to send:
Optional 'thank-you' note:
Send
@Stephan, that deserved a +1 for showing me that
replaceAll
method alone.
You can make it a bit shorter by using
getOrDefault
, although it does then always evaluate
matcher.group()
:
static String fillParameters(String template, Map<?, ?> arguments) { return Pattern.compile("[{]([^}]*?)[}]").matcher(template).replaceAll( match -> arguments.getOrDefault(match.group(1), match.group()).toString()); }
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions
How To Answer Questions
With a little knowledge, a
cast iron skillet
is non-stick and lasts a lifetime.
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
My page always need click reload to connect database
i have post all code please help i am still unable to rectify.
i have edited now tell me where i am wrong
Struts Problem with DAO..
database connectivity error
More...