Skip to content

Commit

Permalink
code review fix
Browse files Browse the repository at this point in the history
  • Loading branch information
lyonlu13 committed Oct 16, 2024
1 parent 67ba812 commit 7541fc8
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ export const StructInput: FC<InputProps> = (props) => {
typeDefinition: { literalType },
value = '',
hasCollectionParent,
initialValue,
} = props;

const { jsonFormRenderable, parsedJson } = useMemo(() => {
Expand Down Expand Up @@ -101,16 +100,6 @@ export const StructInput: FC<InputProps> = (props) => {
setParamData(formData);
}, []);

useEffect(() => {
if (!jsonFormRenderable && initialValue) {
const value = initialValue.scalar?.binary?.value;
if (value) {
const parsedValue = msgpack.decode(value);
onChange(JSON.stringify(parsedValue));
}
}
}, [jsonFormRenderable, initialValue]);

return jsonFormRenderable ? (
<StyledCard error={error} label={label} name={name}>
<StyledForm
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Protobuf from '@clients/common/flyteidl/protobuf';
import Core from '@clients/common/flyteidl/core';
import * as msgpack from '@msgpack/msgpack';
import { InputType, InputValue } from '../types';
import { structPath } from './constants';
import { ConverterInput, InputHelper, InputValidatorParams } from './types';
Expand Down Expand Up @@ -90,9 +91,25 @@ function objectToProtobufStruct(obj: Dictionary<any>): Protobuf.IStruct {
return { fields };
}

function parseBinary(binary: Core.IBinary): string {
if (!binary.value) {
throw new Error('Binary value is empty');
}

if (binary.tag === 'msgpack') {
return JSON.stringify(msgpack.decode(binary.value));
}

// unsupported binary type, it might be temporary
return '';
}

function fromLiteral(literal: Core.ILiteral): InputValue {
const structValue = extractLiteralWithCheck<Protobuf.IStruct>(literal, structPath);
if (literal.scalar?.binary) {
return parseBinary(literal.scalar.binary);
}

const structValue = extractLiteralWithCheck<Protobuf.IStruct>(literal, structPath);
const finalValue = formatParameterValues(InputType.Struct, protobufStructToObject(structValue));
return finalValue;
}
Expand Down
9 changes: 8 additions & 1 deletion packages/oss-console/src/components/Literals/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,14 @@ function processBinary(binary?: Core.IBinary | null) {
return 'invalid binary';
}

if (tag === 'msgpack' && binary.value) {
if (!binary.value) {
return {
tag: `${tag}`,
value: '(empty)',
};
}

if (tag === 'msgpack') {
return {
tag: 'msgpack',
value: msgpack.decode(binary.value),
Expand Down

0 comments on commit 7541fc8

Please sign in to comment.