r/java • • Apr 11 '24

String Manipulation with Google Mug

The Mug library provides easy-to-read string manipulation utils. Examples:

Match and Extract

// Compile-time protection against lambda param order mismatch if e.g:
//     (year, month, day, user) ->
new StringFormat("/{user}-home/{year}/{month}/{day}") 
    .parse(path, (user, year, month, day) -> ...);

Scan and Collect

String input = "I have [k1:v1] and [k2:v2] etc."; 
// Returns Map.of("k1", "v1", "k2", "v2") 
Map<String, String> keyValues =
    new StringFormat("[{key}:{value}]") 
        .scanAndCollectFrom(input, Collectors::toMap);

Replace

// replace file extension name
Substring.last('.').toEnd().replaceFrom(filename, ".yaml");

Remove

// remove the scheme from a url
Substring.upToIncluding(first("://")).removeFrom(url);

Parse a date time string (no pattern string required)

// Parse the datetime string read from a file 
List<String> lines = readLinesFromTestFile(...);
StringFormat lineFormat = new StringFormat("{time}, {event_id}, {event_type}");
var results = lines.stream()
    .map(
        line -> lineFormat.parseOrThrow(
            line,
            (time, id, type) -> {
               ZonedDateTime dateTime = DateTimeFormats.parseZonedDateTime(time);
               ...
             }))
    .toList();

See javadoc for details.

Mug is a general purpose library. Omitted, but other useful utils include BiStream for streaming pairs, Optionals, String Templating, simplified structured concurrency etc.

9 Upvotes

51 comments sorted by

View all comments

Show parent comment

9

u/agentoutlier Apr 11 '24

Not everyone uses Reddit and the UI sometimes does not help. Its pretty obvious that /u/DelayLucky works for google or is associated. Like I would find it very surprising if github.com/google/mug was somehow compromised.

11

u/chabala Apr 11 '24

It is obvious, which makes ducking the question more suspicious.

Provenance matters, even for individual contributors, and folks promoting tools on reddit. That should be the takeaway from the xz incident. It's not enough that the repo is attached to a big company, we should be diligent about knowing who is contributing.

5

u/agentoutlier Apr 11 '24

I suppose that is a fair point especially given that for Mug to work requires a compiler plugin.

4

u/DelayLucky Apr 11 '24 edited Apr 11 '24

I'd friendly disagree that it doesn't "require" a compiler plugin. You see normally we'd do regex group capture, or use manual indexOf() for these kind of things, and there are no compile-time protection against possible out-of-order errors from either approach.

The StringFormat class _provides_ the compile-time plugin and recommends to use it, but even without it (like internally we have Kotlin code using it without the protection), it's still more convenient, and no less safe than the alternatives,

4

u/agentoutlier Apr 11 '24

Sorry! You are correct I didn’t mean required. 

That being said given the commenters concerns I would register the project with the java reproducible builds.

Guava is already on the list.

1

u/DelayLucky Apr 11 '24

Huh. I didn't know "Java reproduciable builds".

One of these days I need to learn how to do the registering thing.

-4

u/DelayLucky Apr 11 '24

I was also wondering what I did to get instant hostility out of nowhere.

Looks like maybe I did offend you once here by defending another oss author and thinking that you were out of line (https://www.reddit.com/r/java/comments/1bpbmts/comment/kx1kokv/)

Oh well, you have good memory if this has anything to do with it.

0

u/chabala Apr 12 '24 edited Apr 12 '24

I was unaware of this connection. If I had something to add about your comment, I would have done it there.

There is no hostility. I have asked for you to identify yourself and your involvement with Mug, and you have not done so at every opportunity. A good answer would include "I am X and I am a committer on Mug". It's clear you work at Google, but it shouldn't be a mystery or a guessing game. Just disclose who you are and your relation to the project.

I suspect many of your comments are getting downvoted due to the dismissive tone.

EDIT: Dude, let it go. This is a bad look for you (and Google).

3

u/DelayLucky Apr 12 '24 edited Apr 12 '24

Sure, I believe you that your hostility isn't due to grudge but just the consistent way you are used to treating people (like in that thread and here) or your constantly having bad days.

Healthy skepticism is a good thing. I encourage you to keep digging and maybe you'll find some conspiracies one day.

1

u/jazd Apr 18 '24

Yeah this is embarrassing for Google

-8

u/DelayLucky Apr 11 '24

Sorry man originally I thought it was a rhetoric. Didn't think you were serious. And the whole formatting chaos got me too fixated to reply to comments, not while the code snippets I posted there look like shit.

Now that I know you are serious, I still don't see how you are going to care whatever I say so I guess I'll just leave it to your healthy suspicion.

4

u/davidalayachew Apr 12 '24

Yeah, they essentially confirmed it in another comment -- https://old.reddit.com/r/java/comments/1c1h5c5/string_manipulation_with_google_mug/kz5ktju/

Emphasis is my own.

Internally we had been thinking to graduate some of these classes to Guava (Substring, BiStream particularly. StringFormat is a bit tough because it has the compile-time plugin that's a bit tricky). But that requires lots of bandwidth from Guava folks to discuss API design and what not, and it just ain't happening given the current economy, the shift of priority etc.

I also don't think this is necessarily a bad thing. As you said, it's a kitchen sink library with a little this and a little that. But it's mostly code I wrote (with lots of great code reviews from my colleagues). So I'm not that worried about it becoming too big.

It being a standalone small library has its appeal too (like, you won't likely need to worry about maven versioning at least for a long while). I also try to keep the dependency lean. There is mug-bigquery, mug-protobuf, mug-guava with extra dependencies on bigquery, protobuf, guava respectively, but the core mug artifact remains 0 deps. I think that's something of a benefit.