nastra commented on code in PR #6489:
URL: https://github.com/apache/iceberg/pull/6489#discussion_r1065453696


##########
core/src/main/java/org/apache/iceberg/rest/auth/JWT.java:
##########
@@ -0,0 +1,65 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.iceberg.rest.auth;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import java.time.Instant;
+import java.util.Base64;
+import java.util.Optional;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.util.JsonUtil;
+import org.immutables.value.Value;
+
[email protected]

Review Comment:
   I understand your argument about potentially over-using `@Immutable`. 
However, I think there's rarely a case where the non-`@Immutable` version is 
better than the `@Immutable` one. In fact I initially put the JWT parsing code 
first into a method, then extracted it into a non-`@Immutable` class like the 
one below and eventually made it `@Immutable`. 
   The non-immutable version of that class will be longer due to boiler-plate 
code. Technically the constructor might even have a precondition null check for 
the token (which we don't need in this particular example, because we already 
know the token is non-null from the static method). In the `@Immutable` we get 
this stuff for free because the API definition is explicit (aka the `token` 
isn't marked as optional or nullable and therefore must always be non-null).
   
   ```
   public class NonImmutableJwt {
   
     private final String token;
     private final long expiresAtEpochMillis;
   
     private Jwt(String token, long expiresAtEpochMillis) {
       this.token = token;
       this.expiresAtEpochMillis = expiresAtEpochMillis;
     }
   
     public String token() {
       return token;
     }
   
     public long expiresAtEpochMillis() {
       return expiresAtEpochMillis;
     }
   
     public long expiresInMillis() {
       return expiresAtEpochMillis() - Instant.now().toEpochMilli();
     }
   
     public boolean isExpired() {
       return expiresAtEpochMillis() <= Instant.now().toEpochMilli();
     }
   
     static Optional<Jwt> of(String token) {
       try {
         Preconditions.checkArgument(null != token, "Invalid JWT: null");
         String[] parts = token.split("\\.");
         Preconditions.checkArgument(parts.length == 3, "Invalid JWT: %s", 
token);
   
         // Parse the payload JSON
         JsonNode jsonNode = 
JsonUtil.mapper().readTree(Base64.getUrlDecoder().decode(parts[1]));
         Long expiresAt = JsonUtil.getLongOrNull("exp", jsonNode);
         return Optional.of(
             new Jwt(
                 token,
                 null == expiresAt
                     ? Long.MAX_VALUE
                     : Instant.ofEpochSecond(expiresAt).toEpochMilli()));
       } catch (Exception e) {
         return Optional.empty();
       }
     }
   }
   ```
   If you're strong on this one, then I can change this to a non-`@Immutable` 
version but as I've tried to outline above, I don't think there's any advantage 
in doing so.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to