0

I would like to ask if there's a way to parse a version number using a groovy script.

I extract from Ariba a payload, the issue comes with a specific field called ItemNumber.

At first it was working, but this month I started to retrieve a version instead of a float.

This is the part of the script that needs to be changed, but I can't find a way to do it.

if (ItemNumber?.trim()){
    list.ItemNumber = Double.parseDouble(ItemNumber.toString());
}

EDIT: This is the field I retrieve: { "ItemNumber": "4.4.5" }.

I would like to get this: { "ItemNumber" : 4.4.5 }.

Any help is greatly appreciated,

Thank you, Kostas

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
  • So what exactly does the item number look like? What do you want to do with it? – Pointy Dec 06 '22 at 14:41
  • This is the field I retrieve: { "ItemNumber": "4.4.5" } I would like to get this: { "ItemNumber" : 4.4.5 } – Konstantinos Tektonidis Dec 06 '22 at 15:00
  • "This is the field I retrieve: { "ItemNumber": "4.4.5" } I would like to get this: { "ItemNumber" : 4.4.5 }" - FYI... I think having multiple decimal points in the input may complicate your solution. I deleted my answer because it won't work with that. – Jeff Scott Brown Dec 06 '22 at 15:30
  • 1
    "This is the field I retrieve: { "ItemNumber": "4.4.5" }" - I think you should add that to the question as some solutions that folks will consider obvious might not work for that. – Jeff Scott Brown Dec 06 '22 at 15:36
  • You can't have a numeric value with two decimal points. You're going to have to keep it as a string, or else break it up into some kind of multi-part value.' – Pointy Dec 06 '22 at 16:56

1 Answers1

0

To parse a version number, you will need to tokenize the string. You should create a Version class to hold the version information. This can help when sorting a list of versions. After tokenizing, you can call the Version constructor, and pass the tokens as integers.

Once you have a Version object, you can access fields like major, minor, and patch.

class Version {
   int major
   Integer minor, patch
   @Override String toString() {
       return [major, minor, patch].findAll().join('.')
   }
}

def parseVersion(String versionString) {
    if (!versionString) return null
    int[] tokens = versionString.split(/\./).collect { it as int }
    return new Version(
        major: tokens[0],
        minor: tokens.length > 1 ? tokens[1] : null,
        patch: tokens.length > 2 ? tokens[2] : null,
    )
}

class Payload {
   String ItemNumber
}

Payload payload = new Payload(ItemNumber: "2.4")
Version version = parseVersion(payload.ItemNumber?.trim())
printf("Major version : %d%n", version.major)
printf("Minor version : %s%n", version.minor ?: "<UNSET>")
printf("Patch version : %s%n", version.patch ?: "<UNSET>")
printf("Full version  : %s%n", version)

In later versions of Groovy, you can call the Versions constructor like this:

new Version(major: tokens[0], minor: tokens?[1], patch: tokens?[2])
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
  • "In later versions of Groovy, you can call the Versions constructor like this" - How far back in Groovy versions do you have to go before that constructor invocation won't work? – Jeff Scott Brown Dec 06 '22 at 17:55
  • @JeffScottBrown I was using [v2.4.8](https://www.tutorialspoint.com/execute_groovy_online.php) – Mr. Polywhirl Dec 06 '22 at 20:25
  • @Mr.Polywhirl, it works! but I can't adjust it in the groovy script, could kindly assist me on this? import com.sap.gateway.ip.core.customdev.util.Message; import java.util.HashMap; import groovy.json.* def Message processData(Message message) { def body = message.getBody(String.class); def jsonSlurper = new JsonSlurper() def list = jsonSlurper.parseText(body) def ItemNumber = list.get("ItemNumber") if (ItemNumber?.trim()) {list.ItemNumber = Double.parseDouble(ItemNumber.toString());} def jsonOP = JsonOutput.toJson(list) message.setBody(jsonOP) return message;} – Konstantinos Tektonidis Dec 07 '22 at 11:27
  • @KonstantinosTektonidis, what's the expected result after transformation? and it sounds like a new question... – daggett Dec 07 '22 at 12:39